feat:添加客户端自定义异常

This commit is contained in:
haoxr 2020-11-19 20:07:50 +08:00
parent 095427cace
commit 63f943f56f
3 changed files with 31 additions and 10 deletions

View File

@ -6,10 +6,14 @@ import com.youlai.auth.service.UserDetailsServiceImpl;
import com.youlai.common.core.constant.AuthConstants;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
@ -119,12 +123,4 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
return accessToken;
};
}
/* @Bean
public AuthenticationProvider daoAuthenticationProvider(){
DaoAuthenticationProvider impl = new DaoAuthenticationProvider();
impl.setUserDetailsService(userDetailsService);
impl.setHideUserNotFoundExceptions(false) ;
return impl ;
}*/
}

View File

@ -5,11 +5,15 @@ import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointR
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import javax.servlet.http.HttpServletResponse;
@Configuration
@EnableWebSecurity
@ -40,4 +44,5 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
public PasswordEncoder passwordEncoder() {
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}
}

View File

@ -4,6 +4,7 @@ import com.youlai.common.core.result.Result;
import com.youlai.common.core.result.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ -13,8 +14,21 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
public class AuthExceptionHandler {
/**
* 客户端信息错误
*
* @param e
* @return
*/
@ExceptionHandler(UsernameNotFoundException.class)
public Result handleUsernameNotFoundException(UsernameNotFoundException e) {
return Result.error(e.getMessage());
}
/**
* 用户名和密码异常
*
* @param e
* @return
*/
@ -26,6 +40,7 @@ public class AuthExceptionHandler {
/**
* 账户异常(禁用锁定过期)
*
* @param e
* @return
*/
@ -39,4 +54,9 @@ public class AuthExceptionHandler {
public Result handleInvalidTokenException(InvalidTokenException e) {
return Result.custom(ResultCode.TOKEN_INVALID_OR_EXPIRED);
}
@ExceptionHandler(Exception.class)
public Result handleException(Exception e) {
return Result.error(e.getMessage());
}
}