Spring Security中的多个入口点不起作用。对于二阶,它给出了禁止的错误

时间:2019-09-03 10:37:39

标签: spring-security-rest

在下面的代码中,我想对UserDetailsS​​ervice使用不同的服务,并对2个不同的URL进行自定义身份验证。以下代码适用于网址“ / api / abc / v1 / ”。当我尝试使用“ / api / xyz / v1 / ”时,它给出了禁止的错误。请有人帮助我的代码。这两个班都在同一班下。

@Configuration
@Order(1)
public static class CMSSecurity extends WebSecurityConfigurerAdapter {

    @Resource(name = "userService")
    private UserDetailsService userDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.antMatcher("/api/abc/v1/**")
        .authorizeRequests().anyRequest().hasRole("USER")
        .and().httpBasic().authenticationEntryPoint(authEntryPoint);
    }

    @Autowired
    public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
    }

    @Autowired
    private AuthenticationEntryPoint authEntryPoint;
}

@Configuration
@Order(2)
public static class FactorySecurity extends WebSecurityConfigurerAdapter {

    @Resource(name = "factoryService")
    private UserDetailsService userDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.antMatcher("/api/xyz/v1/**")
        .authorizeRequests().anyRequest().hasRole("USER")
        .and().httpBasic().authenticationEntryPoint(authEntryPoint);
    }

    @Autowired
    public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder());
    }

    @Autowired
    private AuthenticationEntryPoint authEntryPoint;
}

0 个答案:

没有答案