如何使用Spring Security配置不同的路径?

时间:2019-05-14 10:55:55

标签: java spring spring-boot spring-security

我一直在努力为自己拥有的一些不同路径配置安全性。

我想要这个结构:

/actuator/health <-- open
/api/** <-- hasAnyAuthority
/auth/** <-- basic authentication
all others no access

到目前为止,这就是我所拥有的

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SpringSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .antMatcher("/**") // If you want to override the security provided by Spring Boot.
            .addFilter(preAuthFilter())
            .cors()
                .and()
            .csrf().disable()
            .authorizeRequests()
                .antMatchers("/actuator/health").permitAll()
                .antMatchers("/api/**").hasAnyAuthority("something")
                .antMatchers("/auth/**").authenticated()
                .and()
            .httpBasic();
    }

我想添加.anyRequest().denyAll(),但是在httpBasic()之后似乎无法实现。

任何人都可以确认上面的代码与我想要的相同吗?

1 个答案:

答案 0 :(得分:0)

有关如何按路径拆分配置的示例:

@Configuration
public class ApiSecurityConfiguration extends WebSecurityConfigurerAdapter{

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.antMatcher("/api/**")
            .authorizeRequests()
                .antMatchers("/api/public/**", "/api/register").anonymous() //if you need to allow some path in api
                .antMatchers("/api/**", "/api/register/**").hasRole("API_USER")
            .and()
                .formLogin()
                    .loginPage("/api/")
                    .failureHandler(failureHandler())
                    .loginProcessingUrl("/api/login")
                    .usernameParameter("username")
                    .passwordParameter("password")
                    .successHandler(successHandler())
            .and()
                .logout()
                    .logoutUrl("/api/logout")
                    .logoutSuccessUrl("/api/")
                    .invalidateHttpSession(true)
            .and()
                .rememberMe()
                    .key("something")
            .and()
                .csrf().disable()
            .exceptionHandling()
                .accessDeniedPage("/api/loginfailed");
    }
}

第二条路径:

@Configuration
public class AuthSecurityConfiguration extends WebSecurityConfigurerAdapter{

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.antMatcher("/auth/**")
            .authorizeRequests()
                .antMatchers("/auth/register").anonymous()
                .antMatchers("/auth/**", "/auth/register/**").hasRole("USER")
            .and()
                .formLogin()
                    .loginPage("/auth/")
                    .failureHandler(failureHandler())
                    .loginProcessingUrl("/auth/login")
                    .usernameParameter("username")
                    .passwordParameter("password")
                    .successHandler(successHandler())
            .and()
                .logout()
                    .logoutUrl("/auth/logout")
                    .logoutSuccessUrl("/auth/")
                    .invalidateHttpSession(true)
            .and()
                .rememberMe()
                    .key("something")
            .and()
                .csrf().disable()
            .exceptionHandling()
                .accessDeniedPage("/auth/loginfailed");
    }
}

现在,由于您尚未为/actuator/health添加安全性,因此可以不加保护,也可以为其制造另一个适配器并允许所有人访问。

还应该使用csrf保护,这很容易实现。