SpringSecurity 常用配置

SpringSecurity

配置詳解

登錄配置

protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            .anyRequest().authenticated() //1
            .and()
        .formLogin() //2
            .and()
        .httpBasic(); //3
} 

1.確保我們應用中的所有請求都需要用戶被認證

2.允許用戶進行基於表單的認證

3.允許用戶使用HTTP基於驗證進行認證

java配置使用and()方法相當於XML標籤的關閉

  protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login") //1
            .permitAll();//2        
}

1.指定登錄頁的路徑

2.我們必須允許所有用戶訪問我們的登錄頁(例如爲驗證的用戶),這個formLogin().permitAll()方法允許基於表單登錄的所有的URL的所有用戶的訪問。

驗證請求

protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()   //1                                                             
            .antMatchers("/resources/**", "/signup", "/about").permitAll()       //2           
            .antMatchers("/admin/**").hasRole("ADMIN")    //3                                  
            .antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')")       //4     
            .anyRequest().authenticated()    //5                                            
            .and()
        // ...
        .formLogin();
}

1.http.authorizeRequests()方法有多個子節點,每個macher按照他們的聲明順序執行。

2.我們指定任何用戶都可以通過訪問的多個URL模式。任何用戶都可以訪問URL以”/resources/”, equals “/signup”, 或者 “/about”開頭的URL。

3.以 “/admin/” 開頭的URL只能由擁有 “ROLE_ADMIN”角色的用戶訪問。請注意我們使用 hasRole 方法,沒有使用 “ROLE_” 前綴.

4.任何以”/db/” 開頭的URL需要用戶同時具有 “ROLE_ADMIN” 和 “ROLE_DBA”。和上面一樣我們的 hasRole 方法也沒有使用 “ROLE_” 前綴.
5.尚未匹配的任何URL要求用戶進行身份驗證

處理登出

當使用WebSecurityConfigurerAdapter, 註銷功能會自動啓用。默認是訪問URL/logout將註銷登陸的用戶:

1.使HTTP Session 無效

2.清除所有已經配置的 RememberMe 認證

3.清除SecurityContextHolder

4.跳轉到 /login?logout

protected void configure(HttpSecurity http) throws Exception {
    http
        .logout()      // 1                                                          
            .logoutUrl("/my/logout")  //2                                               
            .logoutSuccessUrl("/my/index")  //3                                         
            .logoutSuccessHandler(logoutSuccessHandler) //4                              
            .invalidateHttpSession(true)      //5                                       
            .addLogoutHandler(logoutHandler)  //6                                        
            .deleteCookies(cookieNamesToClear)   //7                                    
            .and()
        ...
}

1.提供註銷支持,使用WebSecurityConfigurerAdapter會自動被應用。

2.設置觸發註銷操作的URL (默認是/logout). 如果CSRF內啓用(默認是啓用的)的話這個請求的方式被限定爲POST。

3.註銷之後跳轉的URL。默認是/login?logout。

4.讓你設置定製的 LogoutSuccessHandler。如果指定了這個選項那麼logoutSuccessUrl()的設置會被忽略。

5.指定是否在註銷時讓HttpSession無效。 默認設置爲 true。 在內部配置SecurityContextLogoutHandler選項。

6.添加一個LogoutHandler.默認SecurityContextLogoutHandler會被添加爲最後一個LogoutHandler。

7.允許指定在註銷成功時將移除的cookie。

LogoutHandler接口實現類
AbstractRememberMeServices //
CompositeLogoutHandler //
CookieClearingLogoutHandler //
CsrfLogoutHandler //
PersistentTokenBasedRememberMeServices //
SecurityContextLogoutHandler //
TokenBasedRememberMeServices //

驗證

內存中的身份驗證

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth
        .inMemoryAuthentication()
            .withUser("user").password("password").roles("USER").and()
            .withUser("admin").password("password").roles("USER", "ADMIN");
}

將用戶信息配置到內存中進行驗證

JDBC驗證

@Autowired
private DataSource dataSource;

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth
        .jdbcAuthentication()
            .dataSource(dataSource)
            .withDefaultSchema()
            .withUser("user").password("password").roles("USER").and()
            .withUser("admin").password("password").roles("USER", "ADMIN");
}

dataSource 配置數據庫連接信息

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章