Spring Security:一个身份验证和访问控制框架。
简介:
基于Java EE的企业软件应用程序提供全面的安全服务。特别强调支持使用Spring Framework构建的项目。
最小的Spring Security的maven依赖:
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
<!--
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
-->
springboot项目添加该依赖后并直接启动后,访问任何一个url都会弹出验证框。默认用户名为user、密码在控制台显示。
创建Spring Security配置类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.security.config.annotation.authentication.builders.*;
import org.springframework.security.config.annotation.web.configuration.*;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");//为单个用户配置内存身份验证
}
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.and()
.httpBasic();
}
}
-
configureGlobal方法:
①对应用程序中的每个url进行身份验证
②生成登录表单
③允许账户名为user、密码为password的用户使用基于表单的身份验证进行身份验证
④允许用户注销 -
configure(HttpSecurity http)方法:
①确保对我们的应用程序的任何请求都要求用户进行身份验证
②允许用户使用基于表单的登录进行身份验证
③允许用户使用HTTP基本身份验证进行身份验证
④and()类似分隔符 -
默认有个自带的登录作用url,但是大多数程序希望用自己的登录页面:
protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") //指定登录页面的位置 .permitAll(); //授权所有用户登录页面的权限 .failureUrl("/login-error") //指定登录出错请求 }
-
通过http.authorizeRequest()来添加多个子项指定url的自定义要求:
protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() //该方法有多个子节点,每个匹配器按其声明的顺序进行考虑 //url以这几个地址开头的,任何用户都可以访问该请求 .antMatchers("/resources/**", "/signup", "/about").permitAll() //以“/admin/”开头的url都被认为用户具有“ROLE_ADMIN”角色,hasRole方法省略了"ROLE_"前缀 .antMatchers("/admin/**").hasRole("ADMIN") //以“/db/开头的url要求用户同时具有“ROLE_ADMIN”和“ROLE_DBA”角色 .antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')") .anyRequest().authenticated() .and() // ... .formLogin(); }
form-login属性详解
- login-page 自定义登录页url,默认为/login
- login-processing-url 登录请求拦截的url,也就是form表单提交时指定的action (要一致)
- default-target-url 默认登录成功后跳转的url
- always-use-default-target 是否总是使用默认的登录成功后跳转url
- authentication-failure-url 登录失败后跳转的url
- username-parameter 用户名的请求字段 默认为userName
- password-parameter 密码的请求字段 默认为password
- authentication-success-handler-ref 指向一个AuthenticationSuccessHandler用于处理认证成功的请求,不能和default-target-url还有always-use-default-target同时使用
- authentication-success-forward-url 用于authentication-failure-handler-ref
- authentication-failure-handler-ref 指向一个AuthenticationFailureHandler用于处理失败的认证请求
- authentication-failure-forward-url 用于authentication-failure-handler-ref
- authentication-details-source-ref 指向一个AuthenticationDetailsSource,在认证过滤器中使用