如何解决从未调用springboot2 oauth2 AuthenticationEntryPoint
我尝试了在Internet上找到的所有方法,但都无法正常工作。 如找不到用户的错误,请不要调用我自定义的AuthenticationEntryPoint。 它永远不会被调用。 它永远不会被调用。 它永远不会被调用。 它永远不会被调用。 它永远不会被调用。 它永远不会被调用。 它永远不会被调用。 它永远不会被调用。 它永远不会被调用。
我的oauth配置
@Configuration
@EnableAuthorizationServer
public class OAuthConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private ClientDetailsService clientService;
@Autowired
private RedisConnectionFactory connectionFactory;
@Autowired
private WebResponseExceptionTranslator customWebResponseExceptionTranslator;
@Bean
public TokenStore tokenStore() {
return new RedisTokenStore(connectionFactory);
}
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
return new JwtAccessTokenConverter() {
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken,OAuth2Authentication authentication) {
Map<String,Object> infoMap = new HashMap<>();
if (authentication.getUserAuthentication() != null) {
String userName = authentication.getUserAuthentication().getName();
User user = (User) authentication.getUserAuthentication().getPrincipal();
infoMap.put("userName",userName);
infoMap.put("roles",user.getAuthorities());
} else {
infoMap.put("userName",authentication.getOAuth2Request().getClientId());
infoMap.put("roles",authentication.getOAuth2Request().getAuthorities());
}
((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(infoMap);
return super.enhance(accessToken,authentication);
}
};
}
@Override
public void configure(AuthorizationServerSecurityConfigurer security) {
security
.tokenKeyAccess("permitAll()")
.checkTokenAccess("isAuthenticated()")
.allowFormAuthenticationForClients()
.authenticationEntryPoint(new AuthExceptionEntryPoint());
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
endpoints
.authenticationManager(authenticationManager)
.accessTokenConverter(jwtAccessTokenConverter())
.tokenStore(tokenStore())
.exceptionTranslator(customWebResponseExceptionTranslator);
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.withClientDetails(clientService);
}
}
我的资源配置
@Configuration
@EnableResourceServer
public class ResourceConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll().and()
.exceptionHandling().authenticationEntryPoint(new AuthExceptionEntryPoint());
}
}
我的安全配置
@Configuration
@EnableWebSecurity(debug = true)
@EnableGlobalMethodSecurity(securedEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public UserDetailsService userDetailsService() {
return new UserService();
}
@Bean
public UserDetailsService mobileUserDetailsService() {
return new MobileService();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Autowired
private JzLoginFailureHandler loginFailureHandler;
@Autowired
private JzLoginSuccessHandler loginSuccessHandler;
@Autowired
public void globalUserDetails(AuthenticationManagerBuilder auth) {
auth.authenticationProvider(mobileAuthenticationProvider());
auth.authenticationProvider(daoAuthenticationProvider());
}
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.exceptionHandling().authenticationEntryPoint(new AuthExceptionEntryPoint()).and()
.addFilterBefore(getMobileLoginAuthenticationFilter(),UsernamePasswordAuthenticationFilter.class)
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS).permitAll()
.antMatchers("/actuator/**").permitAll()
.antMatchers("/v2/api-docs").permitAll()
.antMatchers("/swagger-resources/**").permitAll()
.antMatchers("/webjars/**").permitAll()
.antMatchers("/oauth/**").permitAll()
.antMatchers("/token").permitAll()
.antMatchers("/user").permitAll()
.anyRequest().authenticated().and()
.httpBasic().and()
.csrf().disable();
}
/**
* 手机验证码登陆过滤器
*
* @return
*/
@Bean
public MobileLoginAuthenticationFilter getMobileLoginAuthenticationFilter() {
MobileLoginAuthenticationFilter filter = new MobileLoginAuthenticationFilter();
try {
filter.setAuthenticationManager(this.authenticationManagerBean());
} catch (Exception e) {
e.printStackTrace();
}
filter.setAuthenticationSuccessHandler(loginSuccessHandler);
filter.setAuthenticationFailureHandler(loginFailureHandler);
return filter;
}
@Bean
public MobileAuthenticationProvider mobileAuthenticationProvider() {
MobileAuthenticationProvider provider = new MobileAuthenticationProvider();
provider.setUserDetailsService(mobileUserDetailsService());
provider.setHideUserNotFoundExceptions(false);
return provider;
}
@Bean
public DaoAuthenticationProvider daoAuthenticationProvider() {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setUserDetailsService(userDetailsService());
provider.setHideUserNotFoundExceptions(false);
provider.setPasswordEncoder(passwordEncoder());
return provider;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。