2014-07-15 47 views
0

对我有弹簧安全在我的应用确保了一些路径,并把其他人打开匿名访问。我遇到的问题与我将访问权限保留为“permitAll”的开放部分相关。我只想保护特定的路径免受非ADMIN的访问,但我希望管理员用户在路径的开放部分时能够被识别。春季安全主体空/用户没有登录permitAll路径

Thymeleaf模板(部分):

<p>Hello Spring Boot User <span th:text="${username}"/>!</p> 
<div sec:authorize="isAnonymous()">isAnonymous</div> 
<div sec:authorize="isRememberMe()">isRememberMe</div> 
<div sec:authorize="isAuthenticated()">isAuthenticated</div> 
<div sec:authorize="isFullyAuthenticated()">isFullyAuthenticated</div> 

注:该模型的用户名定义为:

String username = (principal != null ? principal.getName() : "ANONYMOUS"); 

配置(基于Java) - 有正在使用多种类型的认证的

@Configuration 
public static class FormLoginConfigurationAdapter extends WebSecurityConfigurerAdapter { 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.antMatcher("/form/**").authorizeRequests().anyRequest().authenticated() 
       .and().formLogin().permitAll().loginPage("/form/login").loginProcessingUrl("/form/login") 
       .and().logout().logoutUrl("/form/logout").invalidateHttpSession(true).logoutSuccessUrl("/"); 
    } 
} 

@Order(45) // LOW 
@Configuration 
public static class BasicAuthConfigurationAdapter extends WebSecurityConfigurerAdapter { 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.antMatcher("/basic/**").authorizeRequests().anyRequest().authenticated() 
       .and().httpBasic(); 
    } 
} 

没有SECURI ty配置/打开路径。只是一个控制器。/form路径还有一个控制器。这些都像这样(只是路径现在不同):

@Controller 
@RequestMapping("/open") 
public class OpenController extends BaseController { 
    @RequestMapping({"", "/"}) 
    public String home(HttpServletRequest req, Principal principal, Model model) { 
     commonModelPopulate(req, principal, model); 
     return "home"; // name of the template 
    } 
} 

如果我去这个路径/开放(未保护的)我看到:

Hello Spring Boot User ANONYMOUS! 

但是,如果我去这条道路/形式(表单登录保护 - 登录后)我看到:

Hello Spring Boot User admin! 
isAuthenticated 
isFullyAuthenticated 

所以我觉得可能有多个问题 这里。首先是thymeleaf sec:授权属性没有做任何事情,其次是我似乎只能访问委托人和其他安全信息,如果我在受保护的路径下。

是否有保护只是一个路径(和子路径),但允许在我的应用程序在其他地方访问的主要数据和安全数据的方法吗?

回答

3

你“/打开”资源没有映射到任何安全过滤器。它看起来像你对我需要一个默认的WebSecurityConfigurer(默认路径模式“/ **”),并标记为permitAll()。它应该有比其他更高的@Order,因此它可以作为后备。

@Order(67) // LOWEST 
@Configuration 
public static class NoAuthConfigurationAdapter extends WebSecurityConfigurerAdapter { 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.antMatcher("/**").authorizeRequests().anyRequest().permitAll(); 
    } 
} 
+0

我偶然发现,通过一些试验和错误此解决方案,但它是伟大的,有这样的验证。谢谢! –

0

要访问spring安全基础结构,需要在所有路径(/ *)上配置spring安全筛选器。您不需要许可证全部http配置。您可以在web.xml(http://docs.spring.io/spring-security/site/docs/3.0.x/reference/security-filter-chain.html)或编程式(http://docs.spring.io/spring-security/site/docs/3.2.4.RELEASE/apidocs/org/springframework/security/web/context/AbstractSecurityWebApplicationInitializer.html)中添加过滤器。

+1

“您可以在web.xml中或以编程方式添加过滤器。”但是...怎么样? –