Search in sources :

Example 1 with ResultException

use of plus.wcj.heifer.boot.common.exception.ResultException in project heifer by galaxy-sea.

the class BeanConfig method daoAuthenticationProvider.

@Bean
public AuthenticationProvider daoAuthenticationProvider(PasswordEncoder passwordEncoder) {
    DaoAuthenticationProvider impl = new DaoAuthenticationProvider();
    impl.setPasswordEncoder(passwordEncoder);
    impl.setUserDetailsService(username -> {
        throw new ResultException(ResultStatusEnum.INTERNAL_SERVER_ERROR);
    });
    return impl;
}
Also used : DaoAuthenticationProvider(org.springframework.security.authentication.dao.DaoAuthenticationProvider) ResultException(plus.wcj.heifer.boot.common.exception.ResultException) Bean(org.springframework.context.annotation.Bean)

Example 2 with ResultException

use of plus.wcj.heifer.boot.common.exception.ResultException in project heifer by galaxy-sea.

the class BeanConfig method configure.

@Override
protected void configure(HttpSecurity http) throws Exception {
    // @formatter:off
    http.cors().configurationSource(request -> new CorsConfiguration().applyPermitDefaultValues()).and().csrf().disable().formLogin().disable().httpBasic().disable().authorizeRequests().anyRequest().authenticated().and().logout().disable().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().exceptionHandling().authenticationEntryPoint((request, response, authException) -> {
        throw new ResultException(ResultStatusEnum.UNAUTHORIZED);
    }).accessDeniedHandler((request, response, accessDeniedException) -> {
        throw new ResultException(ResultStatusEnum.FORBIDDEN);
    });
    // @formatter:on
    // 添加自定义 JWT 过滤器
    http.addFilter(new JwtAuthenticationFilter(this.authenticationManager(), this.jwtUtil, this.heiferUserDetailsServiceImpl, handlerExceptionResolver));
}
Also used : JwtUtil(plus.wcj.heifer.boot.common.security.jwt.JwtUtil) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) RequiredArgsConstructor(lombok.RequiredArgsConstructor) AuthenticationProvider(org.springframework.security.authentication.AuthenticationProvider) HashMap(java.util.HashMap) HttpSecurity(org.springframework.security.config.annotation.web.builders.HttpSecurity) JwtAuthenticationFilter(plus.wcj.heifer.boot.common.security.filter.JwtAuthenticationFilter) RequestMappingInfo(org.springframework.web.servlet.mvc.method.RequestMappingInfo) WebSecurityConfigurerAdapter(org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter) HandlerMethod(org.springframework.web.method.HandlerMethod) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) Map(java.util.Map) ResultStatusEnum(plus.wcj.heifer.boot.common.exception.ResultStatusEnum) AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) DelegatingPasswordEncoder(org.springframework.security.crypto.password.DelegatingPasswordEncoder) WebSecurity(org.springframework.security.config.annotation.web.builders.WebSecurity) HttpMethod(org.springframework.http.HttpMethod) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) CorsConfiguration(org.springframework.web.cors.CorsConfiguration) Set(java.util.Set) ResultException(plus.wcj.heifer.boot.common.exception.ResultException) Configuration(org.springframework.context.annotation.Configuration) DaoAuthenticationProvider(org.springframework.security.authentication.dao.DaoAuthenticationProvider) IgnoreProperties(plus.wcj.heifer.boot.common.security.properties.IgnoreProperties) PasswordEncoder(org.springframework.security.crypto.password.PasswordEncoder) HandlerExceptionResolver(org.springframework.web.servlet.HandlerExceptionResolver) EnableWebSecurity(org.springframework.security.config.annotation.web.configuration.EnableWebSecurity) CollectionUtils(org.springframework.util.CollectionUtils) HeiferUserDetailsServiceImpl(plus.wcj.heifer.boot.common.security.userdetails.HeiferUserDetailsServiceImpl) SessionCreationPolicy(org.springframework.security.config.http.SessionCreationPolicy) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) IgnoreWebSecurity(plus.wcj.heifer.boot.common.security.properties.IgnoreWebSecurity) Bean(org.springframework.context.annotation.Bean) JwtAuthenticationFilter(plus.wcj.heifer.boot.common.security.filter.JwtAuthenticationFilter) CorsConfiguration(org.springframework.web.cors.CorsConfiguration) ResultException(plus.wcj.heifer.boot.common.exception.ResultException)

Example 3 with ResultException

use of plus.wcj.heifer.boot.common.exception.ResultException in project heifer by galaxy-sea.

the class JwtUtil method verify.

private void verify(SignedJWT signedJwt, MACVerifier verifier) throws ParseException, JOSEException {
    if (!signedJwt.verify(verifier)) {
        throw new ResultException(ResultStatusEnum.UNAUTHORIZED);
    }
    JWTClaimsSet claimsSet = signedJwt.getJWTClaimsSet();
    // throw new ResultException(ResultStatus.UNAUTHORIZED);
    final Date now = new Date();
    final Date exp = claimsSet.getExpirationTime();
    if (exp == null || !DateUtils.isAfter(exp, now, MAX_CLOCK_SKEW_SECONDS)) {
        throw new ResultException(ResultStatusEnum.EXPIRED_TOKEN);
    }
    final Date nbf = claimsSet.getNotBeforeTime();
    if (nbf == null || !DateUtils.isBefore(nbf, now, MAX_CLOCK_SKEW_SECONDS)) {
        throw new ResultException(ResultStatusEnum.TOKEN_BEFORE_USE_TIME);
    }
}
Also used : JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) ResultException(plus.wcj.heifer.boot.common.exception.ResultException) Date(java.util.Date)

Example 4 with ResultException

use of plus.wcj.heifer.boot.common.exception.ResultException in project heifer by galaxy-sea.

the class AliyunSmsServer method sendCaptcha.

@Override
public void sendCaptcha(String phoneNumbers, String captcha) {
    SendSmsRequest sendSmsRequest = new SendSmsRequest().setPhoneNumbers(phoneNumbers).setSignName(this.aliyunSmsProperties.getSignName()).setTemplateCode(TemplateCode.SMS_185520034.toString()).setTemplateParam("{\"code\":\"" + captcha + "\"}");
    SendSmsResponse sendSmsResponse;
    try {
        sendSmsResponse = this.client.sendSms(sendSmsRequest);
    } catch (Exception e) {
        throw new ResultException(ResultStatusEnum.SMS_NETWORK_EXCEPTION);
    }
    SendSmsResponseBody body = sendSmsResponse.getBody();
    if (!OK.equals(body.getCode())) {
        throw new ResultException(ResultStatusEnum.SMS_SEND_FAIL, body.getMessage());
    }
}
Also used : SendSmsRequest(com.aliyun.dysmsapi20170525.models.SendSmsRequest) SendSmsResponseBody(com.aliyun.dysmsapi20170525.models.SendSmsResponseBody) ResultException(plus.wcj.heifer.boot.common.exception.ResultException) SendSmsResponse(com.aliyun.dysmsapi20170525.models.SendSmsResponse) ResultException(plus.wcj.heifer.boot.common.exception.ResultException)

Example 5 with ResultException

use of plus.wcj.heifer.boot.common.exception.ResultException in project heifer by galaxy-sea.

the class TenantMethodArgumentResolver method resolveArgument.

@SuppressWarnings("NullableProblems")
@Override
public Tenant resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) {
    UserPrincipal userDetails = (UserPrincipal) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    if (userDetails == null) {
        throw new ResultException(ResultStatusEnum.UNAUTHORIZED);
    }
    String allPower = "";
    if (userDetails.getTenantId() != null) {
        allPower = heiferUserDetailsService.getAllPower(userDetails.getTenantId(), userDetails.getId());
    }
    return new Tenant(userDetails.getId(), userDetails.getUsername(), userDetails.getTenantId(), userDetails.getDeptId(), allPower, false);
}
Also used : Tenant(plus.wcj.heifer.boot.extension.tenant.Tenant) ResultException(plus.wcj.heifer.boot.common.exception.ResultException) UserPrincipal(plus.wcj.heifer.boot.common.security.userdetails.dto.UserPrincipal)

Aggregations

ResultException (plus.wcj.heifer.boot.common.exception.ResultException)7 JOSEException (com.nimbusds.jose.JOSEException)2 JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)2 SignedJWT (com.nimbusds.jwt.SignedJWT)2 Date (java.util.Date)2 Bean (org.springframework.context.annotation.Bean)2 DaoAuthenticationProvider (org.springframework.security.authentication.dao.DaoAuthenticationProvider)2 SendSmsRequest (com.aliyun.dysmsapi20170525.models.SendSmsRequest)1 SendSmsResponse (com.aliyun.dysmsapi20170525.models.SendSmsResponse)1 SendSmsResponseBody (com.aliyun.dysmsapi20170525.models.SendSmsResponseBody)1 MACSigner (com.nimbusds.jose.crypto.MACSigner)1 MACVerifier (com.nimbusds.jose.crypto.MACVerifier)1 ParseException (java.text.ParseException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1 EnableConfigurationProperties (org.springframework.boot.context.properties.EnableConfigurationProperties)1 Configuration (org.springframework.context.annotation.Configuration)1 HttpMethod (org.springframework.http.HttpMethod)1