简单的多重安全性现在可以工作了

f2uvfpb9  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(276)

此问题已在此处找到答案

Spring Security :多个http配置不工作(2个答案)
17天前关门了。
我正在处理这个问题,从7小时前开始,我找不到一个解释,为了简单起见,我只是做了一个小一点的例子。我需要一些带有安全访问(jwt)的URL,以及带有表单登录的其他路径( Jmeter 板)。
这是我的代码:

@EnableWebSecurity
public class MultiHttpSecurityConfig {

@Autowired
private UserDetailsService jwtUserDetailsService;

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth
        .userDetailsService(jwtUserDetailsService)
        .passwordEncoder(passwordEncoder());
}

@Bean
public PasswordEncoder passwordEncoder() {
    return NoOpPasswordEncoder.getInstance();
}

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

    @Autowired
    private JwtRequestFilter jwtRequestFilter;

    protected void configure(HttpSecurity http) throws Exception {

        http
            .csrf().disable()
                // Get Request and /Authenticate do not need authentication
            .authorizeRequests()
                .antMatchers("/authenticate", "/authenticate/**").permitAll()
                .antMatchers(HttpMethod.GET, "/api/**").permitAll()
                // all others do need authentication
                .anyRequest().authenticated()
                .and()
            .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        http.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}

@Configuration
public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

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

        http
            .authorizeRequests()
                .antMatchers("/dashboard/index.html").authenticated()
                .and()
            .formLogin();
    }
}

这个例子是可行的,jwt机制非常有效。唯一不起作用的是表单登录。当我点击浏览器时 localhost:8080/dashboard/index.html ,则会显示该文件。
这就是我需要的:
/授权-->任何人都可以点击该url获取jwt令牌
/api-->获取方法不需要授权
/api-->所有其他 predicate 都需要标记。
/dashboard/index.html-->应显示表单登录名。
我知道 anyRequest().authenticated() ,它位于第一个配置中,但如果我甚至对该行进行注解,则第二行 Order 完全被忽视了。
我应该添加或删除什么来实现我的想法?

9q78igpj

9q78igpj1#

在你的 FormLoginWebSecurityConfigurerAdapter 这个 antMatchers() 应该提前打电话 authorizeRequests() -这表示此筛选器链仅应用于请求 /dashboard/index.html .

http.antMatcher("/dashboard/index.html")
    .authorizeRequests()
        .anyRequest().authenticated() // since this filter chain only apply to /dashboard/index.html, don't need use antMatchers() to check again
        .and()
    .formLogin();

有关更多信息:https://docs.spring.io/spring-security/site/docs/current/reference/html5/#multiple-httpsecurity
第二个问题是,您的 FormLoginWebSecurityConfigurerAdapter 必须在之前(少于) ApiWebSecurityConfigurationAdapter . WebSecurityConfigurerAdapter 具有默认的@顺序 100 ,所以您应该进行注解 @Order(0) 在你的 FormLoginWebSecurityConfigurerAdapter .

相关问题