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将注销登陆的用户:

扫描二维码关注公众号,回复: 2370076 查看本文章

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 配置数据库连接信息

猜你喜欢

转载自blog.csdn.net/tanfengshang0872/article/details/80066315