2014-07-15 54 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中或以編程方式添加過濾器。」但是...怎麼樣? –