details在Java中_Spring安全性在Java配置中使用两个自定义UserDetailsS​​ervices

我试图将XML中的实际安全配置传递给Java,但我不知道为什么在两个过滤器中有两个UserdetailsService,过滤器总是使用相同的UserdetailsService。

XML配置:

auto-config="false">

auto-config="false">

auto-config="false">

Java配置:

@EnableWebSecurity

public class SecurityConfig {

@Autowired

private CustomUserAppDetailsService customUserAppDetailsService;

@Autowired

private CustomRefreshTokenAppDetailsService customRefreshTokenAppDetailsService;

@Autowired

private CustomAccessTokenAppDetailsService customAccessTokenAppDetailsService;

@Bean

public TokenAuthenticationEntryPoint tokenAuthenticationEntryPoint(){

return new TokenAuthenticationEntryPoint();

}

@Configuration

@Order(1)

public class RefreshTokenSecurityConfiguration extends WebSecurityConfigurerAdapter {

@Bean

public AuthenticationManager refresTokenAuthenticationManager() throws Exception {

return super.authenticationManagerBean();

}

@Bean

public RefreshTokenAuthenticationFilter refreshTokenAuthenticationFilter(){

return new RefreshTokenAuthenticationFilter();

}

@Override

protected void configure(HttpSecurity http) throws Exception {

DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider ();

authenticationProvider.setUserDetailsService(customRefreshTokenAppDetailsService);

http

.csrf().disable()

.antMatcher("/v1/Login/refresh/**")

.userDetailsService(customRefreshTokenAppDetailsService)

.sessionManagement()

.sessionCreationPolicy(SessionCreationPolicy.STATELESS)

.and()

.exceptionHandling()

.authenticationEntryPoint(tokenAuthenticationEntryPoint())

.and()

.authorizeRequests()

.antMatchers("/v1/Login/refresh/**").access("hasRole('ROLE_USER')")

.and()

.requiresChannel()

.antMatchers("/v1/Login/refresh/**").requiresSecure()

.and()

.addFilterBefore(refreshTokenAuthenticationFilter(), BasicAuthenticationFilter.class);

}

}

@Configuration

@Order(2)

public class LoginSecurityConfiguration extends WebSecurityConfigurerAdapter {

@Override

protected void configure(HttpSecurity http) throws Exception {

DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider ();

authenticationProvider.setUserDetailsService(customUserAppDetailsService);

authenticationProvider.setPasswordEncoder(new Md5PasswordEncoder());

http

.csrf().disable()

.antMatcher("/v1/Login/**")

.authenticationProvider(authenticationProvider)

.sessionManagement()

.sessionCreationPolicy(SessionCreationPolicy.STATELESS)

.and()

.authorizeRequests().antMatchers("/v1/Login/**").access("hasRole('ROLE_USER')")

.and()

.requiresChannel().antMatchers("/v1/Login/**").requiresSecure()

.and()

.httpBasic();

}

}

@Configuration

@Order(3)

public class UsuarioSecurityConfiguration extends WebSecurityConfigurerAdapter {

@Bean

public TokenAuthenticationFilter tokenAuthenticationEmpresaFilter() throws Exception {

TokenAuthenticationFilter tokenAuthenticationFilter = new TokenAuthenticationFilter();

tokenAuthenticationFilter.setAuthenticationManager(super.authenticationManager());

return tokenAuthenticationFilter;

}

@Override

protected void configure(HttpSecurity http) throws Exception {

DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider ();

authenticationProvider.setUserDetailsService(customAccessTokenAppDetailsService);

http

.csrf().disable()

.antMatcher("/v1/UsuarioEmpresa/**")

.authenticationProvider(authenticationProvider)

.userDetailsService(customAccessTokenAppDetailsService)

.sessionManagement()

.sessionCreationPolicy(SessionCreationPolicy.STATELESS)

.and()

.exceptionHandling()

.authenticationEntryPoint(tokenAuthenticationEntryPoint())

.and()

.authorizeRequests()

.antMatchers("/v1/UsuarioEmpresa/**").access("hasRole('ROLE_USER')")

.and()

.requiresChannel()

.antMatchers("/v1/UsuarioEmpresa/").requiresSecure()

.antMatchers("/v1/UsuarioEmpresa/**").requiresSecure()

.and()

.addFilterBefore(tokenAuthenticationEmpresaFilter(), BasicAuthenticationFilter.class);

}

}

}出于某种原因,http配置(顺序1和3)均使用customRefreshTokenAppDetailsService。总是使用第一个http的UserDetailsService。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值