2014-01-12 40 views
0

使用彈簧webmvc和3.2版彈簧安全的web視圖築底,我想返回根據用戶角色的不同觀點(或者是否用戶進行身份驗證或沒有),所以,對於"/"請求角色匿名(或不經過身份驗證的用戶的用戶)獲取歡迎頁和作用用戶的用戶得到頁。春季3.2:選擇用戶角色

我目前的做法與常規控制器這樣做:

@Controller 
public class WelcomeCtrl { 

    @RequestMapping("/") 
    public String welcome(Principal principal) { 
     if (userAuthenticated(principal)) { 
      return "redirect:home"; 
     } 
     return "welcome"; 
    } 

    private boolean userAuthenticated(Principal principal) { 
     return principal != null && principal instanceof Authentication 
       && hasUserRole((Authentication) principal); 
    } 

    private boolean hasUserRole(Authentication principal) { 
     Collection<? extends GrantedAuthority> authorities = (principal) 
       .getAuthorities(); 
     return Iterables.contains(Collections2.transform(authorities, 
       new Function<GrantedAuthority, String>() { 

        @Override 
        public String apply(GrantedAuthority authority) { 
         return authority.getAuthority(); 
        } 
       }), "ROLE_USER"); 
    } 

} 

不過,我真的不喜歡它,因爲我覺得這應該重定向使用Spring Security來完成(我錯了?)。你知道使用Spring Security配置的方法嗎?我目前的配置如下:

@Configuration 
@EnableWebSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests() 
       .antMatchers("/", "/welcome").permitAll() 
       .anyRequest().authenticated() 
      .and().formLogin() 
       .defaultSuccessUrl("/home").permitAll() 
       .and().logout().permitAll(); 
    } 

    @Override 
    protected void configure(AuthenticationManagerBuilder authManagerBuilder) 
      throws Exception { 
     authManagerBuilder.inMemoryAuthentication().withUser("user") 
       .password("password").roles("USER"); 
    } 
} 

回答

0

據我所知,這是使用市場上最新的彈簧安全配置的最好方法。

對Spring配置的一般支持被添加到Spring 3.1的Spring框架中。自Spring Security 3.2以來,已經有了Spring Security Java Configuration支持,使用戶無需使用任何XML即可輕鬆配置Spring Security。

如果你熟悉安全命名空間配置,那麼你會發現它和安全的Java配置支持之間有不少相似之處,你可以使用安全命名空間配置,以及其套房,爲您

有很多新的東西添加到彈簧安全3.2你可以使用 像

1)。 Java配置 - 對於那些更熟悉讀取jar文件來應用配置方法的人來說更好。 2)。併發支持 - 在大多數環境中,安全以每個線程爲基礎進行存儲。這意味着當一個新線程完成工作時,SecurityContext會丟失。 Spring Security提供了一些基礎設施來幫助用戶更容易。 Spring Security在多線程環境中爲Spring Security提供了低級抽象。事實上,這就是Spring Security與AsyncContext.start(Runnable)和Spring MVC Async Integration集成的基礎。 3)。 CSRF攻擊: - 假設您銀行的網站提供了一個表格,允許將當前登錄用戶的錢轉移到另一個銀行賬戶。例如,HTTP請求可能類似於:

POST /transfer HTTP/1.1 
Host: bank.example.com 
Cookie: JSESSIONID=randomid; Domain=bank.abc.com; Secure; HttpOnly 
Content-Type: application/x-www-form-urlencoded 

amount=100.00&routingNumber=1234&account=9876 

現在假裝你身份驗證您的銀行網站,然後在不註銷,請訪問一個邪惡的網站。邪惡的網站包含一個HTML頁面的格式如下:

<form action="https://bank.example.com/transfer" method="post"> 
    <input type="hidden" 
     name="amount" 
     value="100.00"/> 
    <input type="hidden" 
     name="account" 
     value="evilsAccountNumber"/> 
    <input type="submit" 
     value="ohh you Win Money!"/> 
</form> 

你喜歡贏得金錢,讓你點擊提交按鈕。在這個過程中,你無意間將100美元轉讓給惡意用戶。發生這種情況的原因是,雖然惡意網站無法看到您的Cookie,但與您的銀行相關的Cookie仍會與請求一起發送。

這種類型的攻擊可以通過使用彈簧安全措施3輕鬆停止。2,因爲這個新功能跨站點請求僞造(CSRF)保護在spring security中生成了一些在支付網關側匹配的令牌。

和更多優點......................

+0

@Macias你對我的答案滿意嗎,對你來說看起來不錯,我的朋友 – Tenacious

+0

@Macias如果它看起來不錯,請注意它,所以它會對其他人有所幫助我的朋友 – Tenacious

+0

那麼,從春季文檔複製粘貼的確切位置是我的問題的答案? -1爲我的朋友 – macias