Spring Boot Security - 默认映射的匿名用户访问/

时间:2015-02-11 22:00:33

标签: java spring-boot spring-security

我们有基于Spring启动的应用程序,我们想给匿名用户提供默认/映射访问权限。 我们添加了默认index.html(基本页面)。

在控制器

@RequestMapping("/")
public ModelAndView defaultViewManager(HttpServletRequest request) {
    logger.info("Default mapping.");
    ModelAndView modelAndView = new ModelAndView("index");
    return modelAndView;
} 

SecurityConfig

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

private static final String SSO_HEADER = "AUTH_USER";

public static final String ADMIN = "ROLE_ADMIN";
public static final String USER = "ROLE_USER";
public static final String ANONYMOUS = "ROLE_ANONYMOUS";

@Autowired
private PreAuthUserDetailsService userDetailsService;

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.authenticationProvider(preAuthenticatedAuthProvider());
}

@Bean
public PreAuthenticatedAuthenticationProvider preAuthenticatedAuthProvider() {
    UserDetailsByNameServiceWrapper<PreAuthenticatedAuthenticationToken> wrapper =
            new UserDetailsByNameServiceWrapper<PreAuthenticatedAuthenticationToken>    (userDetailsService);

    PreAuthenticatedAuthenticationProvider authProvider = new PreAuthenticatedAuthenticationProvider();
    authProvider.setPreAuthenticatedUserDetailsService(wrapper);
    return authProvider;
}

@Bean
public RequestHeaderAuthenticationFilter headerAuthFilter() throws Exception {
    RequestHeaderAuthenticationFilter filter = new RequestHeaderAuthenticationFilter();
    filter.setPrincipalRequestHeader(SSO_HEADER);
    filter.setAuthenticationManager(authenticationManagerBean());
    return filter;
}

上面提到的代码可能没有必要,但是对于后台,我们使用的是PreAuthenticatedAuthentication Provider

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

    // @formatter:off
    http.addFilter(headerAuthFilter())
        .authorizeRequests()
            .antMatchers("/").permitAll()
            .antMatchers("/admin/**").hasAuthority(ADMIN)
            .antMatchers("/**").hasAuthority(USER)

        .and()
            .logout()
                .deleteCookies("remove")
                .invalidateHttpSession(true)
                .logoutUrl("/logout")
                .logoutSuccessUrl("/login?logout")
        .and()
            .csrf().disable()
            .headers().frameOptions().disable();
    // @formatter:on
}
}

仅供参考,我也添加了拦截器。即使使用排除模式

,拦截器似乎也会被触发
public void addInterceptors(InterceptorRegistry registry) {     
         registry.addInterceptor(wikiRequestHandlerInterceptor()).
excludePathPatterns("/").addPathPatterns("/**");   

}

在上面的SecurityConfig代码中。我尝试允许使用.antMatchers("/").permitAll()并添加权限以进行休息意味着所有/**/admin/**。但这不起作用。请帮助提及正确的antMatchers,以提供对默认/映射的匿名访问。

先谢谢。

1 个答案:

答案 0 :(得分:3)

看起来需要重新安排antMatchers来修复优先级。要在"/"允许“所有请求”首先添加anyRequest().permitAll(),然后添加受限制的目录,最后是全部/**,如下所示:

http.addFilter(headerAuthFilter())
    .authorizeRequests()
    .anyRequest().permitAll()
    .antMatchers("/admin/**").hasAuthority(ADMIN)
    .antMatchers("/**").hasAuthority(USER)

可以设置视图控制器直接映射到模板目录中的indexroot.html(假设是ThymeLeaf):

public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("indexroot");
}

我相信拦截器仍可以用“/”排除,任何顺序:

public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(wikiRequestHandlerInterceptor())
            .addPathPatterns("/admin/**")
            .excludePathPatterns("/");
}