SpringSecurity

SpringSecurity

意义:

  • 功能性需求:否
  • web开发中安全第一位 (过滤器拦截器)
  • 在设计之初就要考虑安全
  • 针对spring项目的安全框架,无缝衔接springboot

主流安全框架:

  • SringSecurity、Shiro:很相似除了类名和名字不一样
  • 主要功能:认证和授权
  • Aop横向切入思想

认证和授权

认证(Authentication)—— 主要是登录功能

  • 身份验证是关于验证您的凭据,如用户名/用户ID和密码,以验证您的身份。

  • 身份验证通常通过用户名和密码完成,有时与身份验证因素结合使用。

授权(Authorization)—— Http界面跳转

  • 授权发生在系统成功验证您的身份后,最终会授予您访问资源(如信息,文件,数据库,资金,位置,几乎任何内容)的完全权限。

springsecurity重要类(用空再写一下设计模式的笔记)

  1. WebSecurityConfigurerAdapter:自定义Security策略,springconfig配置类继承了该类(适配器模式)
  2. HttpSecurity:自定义授权策略(建造者模式)
  3. AuthenticationManagerBuilder:自定义认证策略(建造者模式)
  4. @EnableWebSecurity:开启WebSecurity模式 @EnableXXXX开启某个功能

外部操作流程

  1. 创建springsecurity项目(创建时就可导入Themeleleaf)
  2. 关闭Themeleleaf模板引擎缓存
  3. 导入静态资源(一些前端测试界面,安利一下semantic-ui这个前端框架,layui也不错)
  4. 创建config和Controller文件夹,编辑Controller测试环境是否ok
  5. pom文件中startsecurity,编写一个springsecurity配置类

内部操作流程(Securityconfig类)

1.在SecurityConfig类上加入注解
2.该类继承WebSecurityConfigurerAdapter
3.重写configure(HttpSecurity http)方法 定制授权规则
4.重写configure(AuthenticationManagerBuilder auth)方法 定制认证规则

###总结
1.授权:

  • 用户界面访问授权
  • 登录页面的定制及登录时传递的参数定制(可修改默认登录界面)
  • 注销功能,注销后可跳转到指定界面。如果注销失败则关闭csrf
  • 记住我功能,注意前端传入的参数名

2.认证:(以从内存中获取为例,后面用了数据库再加上)

  • 用户、密码、角色
  • 必须开启加密才可以登录

3.Themeleleaf整合springsecurity

  • pom导入 thymeleaf-security
  • 导入命名空间
  • 未登录时显示登录按钮,登录成功显示用户名和角色信息及注销按钮
  • 角色功能块认证,不同权限显示不同的功能块

4.其它

  • sec标签不提示问题
  • springboot版本使用2.0.x(高版本无法适配)

securityconfig代码

package com.kuang.config;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;


//AOP:橫切思想

@EnableWebSecurity   //开启security模式
public class SecurityConfig  extends WebSecurityConfigurerAdapter {

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		//首页所有人可以访问,功能只有对应权限的人才能访问

		//定制授权规则
		http.authorizeRequests()
				.antMatchers("/").permitAll()
				.antMatchers("/level1/**").hasRole("vip1")
				.antMatchers("/level2/**").hasRole("vip2")
				.antMatchers("/level3/**").hasRole("vip3");

		//没有权限默认会到登录界面(默认登录页面为/login)
		//定制登录页面
		//登录默认传的是password,username
		http.formLogin()
				.loginPage("/toLogin")
				.usernameParameter("user")
				.passwordParameter("pwd");

		//注销功能,注销后跳到首页
		http.logout().logoutSuccessUrl("/");

		//如果注销失败则关闭csrf
		http.csrf().disable();

		//记住我功能  默认时间两周,cookie,接收前端自定义的记住我参数
		http.rememberMe().rememberMeParameter("remember");
	}


	//认证
	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		//可以从数据库中获取
		//也从内存中获取
		//spring security 官方推荐的是使用bcrypt加密方式。
		auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
				.withUser("kuangshen").password(new BCryptPasswordEncoder().encode("981028")).roles("vip2","vip3")
				.and()
				.withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3")
				.and()
				.withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2");
	}
}

其它资源

        <!--thymeleaf模板-->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-java8time</artifactId>
        </dependency>

    <!--thymeleaf-security-->
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
            <version>3.0.4.RELEASE</version>
        </dependency>

    //Themeleleaf整合security
    <div sec:authorize="isAuthenticated()">    //判断是否登录
    <span sec:authentication="principal.username">   //显示用户名
     <div class="column" sec:authorize="hasRole('vip1')">  //角色功能块认证



    //命名空间
    xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5"

Spring Security和Shiro

相同点:

  • 认证功能
  • 授权功能
  • 加密功能
  • 会话管理
  • 缓存支持
  • rememberMe功能…

优点:

1:Spring Security基于Spring开发,项目中如果使用Spring作为基础,配合Spring Security做权限更加方便,而Shiro需要和Spring进行整合开发

2:Spring Security功能比Shiro更加丰富些,例如安全防护

3:Spring Security社区资源比Shiro丰富

缺点:

1:Shiro的配置和使用比较简单,Spring Security上手复杂

2:Shiro依赖性低,不需要任何框架和容器,可以独立运行,而Spring Security依赖于Spring容器


个人感觉springsecurity比shiro好用
其实和其它功能理念差不多,编写一个config横切进去(Aop思想也就是)
学习springsecurity时的小demo Github-springSecurity

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值