Spring Security知识点归纳一

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();
	}
}
  1. configureGlobal方法:
    ①对应用程序中的每个url进行身份验证
    ②生成登录表单
    ③允许账户名为user、密码为password的用户使用基于表单的身份验证进行身份验证
    ④允许用户注销

  2. configure(HttpSecurity http)方法:

    ①确保对我们的应用程序的任何请求都要求用户进行身份验证
    ②允许用户使用基于表单的登录进行身份验证
    ③允许用户使用HTTP基本身份验证进行身份验证
    ④and()类似分隔符

  3. 默认有个自带的登录作用url,但是大多数程序希望用自己的登录页面:

    protected void configure(HttpSecurity http) throws Exception {
    	http
    		.authorizeRequests()
    			.anyRequest().authenticated()
    			.and()
    		.formLogin()
    			.loginPage("/login") //指定登录页面的位置
    			.permitAll(); //授权所有用户登录页面的权限
                .failureUrl("/login-error") //指定登录出错请求
    }
    
  4. 通过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属性详解
  1. login-page 自定义登录页url,默认为/login
  2. login-processing-url 登录请求拦截的url,也就是form表单提交时指定的action (要一致)
  3. default-target-url 默认登录成功后跳转的url
  4. always-use-default-target 是否总是使用默认的登录成功后跳转url
  5. authentication-failure-url 登录失败后跳转的url
  6. username-parameter 用户名的请求字段 默认为userName
  7. password-parameter 密码的请求字段 默认为password
  8. authentication-success-handler-ref 指向一个AuthenticationSuccessHandler用于处理认证成功的请求,不能和default-target-url还有always-use-default-target同时使用
  9. authentication-success-forward-url 用于authentication-failure-handler-ref
  10. authentication-failure-handler-ref 指向一个AuthenticationFailureHandler用于处理失败的认证请求
  11. authentication-failure-forward-url 用于authentication-failure-handler-ref
  12. authentication-details-source-ref 指向一个AuthenticationDetailsSource,在认证过滤器中使用
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值