网站建设|,网站 建设 内容 安排,wordpress验证,沈阳建设银行网站1. 认证授权的概述
1.1 什么是认证 进入移动互联网时代#xff0c;大家每天都在刷手机#xff0c;常用的软件有微信、支付宝、头条、抖音等 以微信为例说明认证的相关基本概念。在初次使用微信前需要注册成为微信用户#xff0c;然后输入账号和密码即可登录微信#xff0c…1. 认证授权的概述
1.1 什么是认证 进入移动互联网时代大家每天都在刷手机常用的软件有微信、支付宝、头条、抖音等 以微信为例说明认证的相关基本概念。在初次使用微信前需要注册成为微信用户然后输入账号和密码即可登录微信输入账户和密码登录微信的过程就是认证 系统为什么需要认证 认证是为了保护系统的隐私数据和资源用户的身份合法方可访问该系统的资源 认证用户认证就是判断一个用户的信息是否合法的过程用户去访问系统资源时系统需要要求验证用户的身份信息身份合法方可继续访问不合法则拒绝访问。常见的用户身份认证方式有用户名和密码登录、二维码登录、手机短信登录、指纹认证等方式 1.2 什么是会话 用户认证通过后为了避免用户的每次操作都进行认证可将用户的信息保证在会话中。会话就是系统为了保持当前用户的登录状态所提供的机制常见的有基于session方式、基于token方式 1.2.1 基于session的认证 它的交互流程是 用户认证成功后在服务端生成用户相关的数据保存在session(当前会话)中发给客户端的session_id存放到cookie中。 这样用户客户端请求时带上session_id就可以验证服务器端是否存在session数据以此完成用户的合法校验当前用户退出系统或session过期销毁时客户端的session_id也就无效了 1.2.2 基于Token的认证 它的交互流程是 用户认证成功后服务端生成一个token(令牌)【唯一字符串】发给客户端客户端可以放到cookie或sessionStorage等存储中每次请求时带上token服务端收到token通过验证后即可确认用户身份 基于session的认证方式由servlet规范定制服务端要存储session信息需要占用内存资源客户端需要支持cookie基于token的方式则一般不需要服务端存储token并且不限制客户端的存储方式cookie sessionStorage LocalStorage Vuex。如今移动互联网时代更多类型的客户端[pC ,android,IOS,]需要接入系统系统多是采用前后端分离的架构进行实现所以基于token的方式更适合 使用前后端分离或后台使用了集群—一定采用token模式。 传统的项目前端和后端都在一个工程下—基于session模式。 1.3 什么是授权
还拿微信来举例子微信登录成功后用户即可使用微信的功能比如发红包、发朋友圈、添加好友等没有绑定银行卡的用户是无法发送红包的绑定银行卡的用户才可以发红包发红包功能、发朋友圈功能都是微信的资源即功能资源用户拥有发红包功能的权限才可以正常使用发送红包功能拥有发朋友圈功能的权限才可以便用发朋友圈功能这个根据用户的权限来控制用户使用资源的过程就是授权。 权限【权限表】----资源【接口】 1.3.1 为什么要授权
认证是为了保证用户身份的合法性授权则是为了更细粒度的对隐私数据进行划分授权是在认证通过后发生的控制不同的用户能够访问不同的资源。 授权:授权是用户认证通过根据用户的权限来控制用户访问资源的过程,拥有资源的访问权限则正常访问没有权限则拒绝访问。 认证授权的框架: [1]shiro 轻量级的认证授权 它可以整合任意框架 它支持javase和javaee [2]springsecurity 重量级的认证授权框架。它只能和spring整合只支持javaee web框架。 spring非常麻烦但是现在和springboot整合就很简单了。 2.概述springsecurity
官网https://spring.io/projects/spring-security#overview
2.1 什么是spring security?
Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在Sprirg应用上下文中配置的Bean充分利用了Spring IoC []DI(控制反转Inversion of Control ,DI:Dependency Injection依赖主入和AOP(面向切面编程功能为应用系统提供声明式的安全访问控制功能减少了为企业系统安全控制编写大量重复代码的工作。 以上解释来源于百度白科。可以一句话来概括SpringSecurity 是一个安全框架。可以帮我们完成认证密码加密授权rememberme的功能【security安全】
3.快速入门springsecurity 基于内存的数据。 引入依赖 !--引入springsecurity的依赖--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-security/artifactId/dependency创建接口资源——创建一个controller
RestController
public class HelloController {GetMapping(/hello)public String hello(){return Hello~~~~~~~~~~~~~~~;}启动项目并访问资源 发现帮你跳转到登录页面。 因为springsecurity包含了很多过滤器认证过滤器发现你没有登录就访问资源。默认调整到它内置的登录页面 账号为: user 密码: 在控制台可以看见 4. 自定义账号和密码
在配置文件中添加配置——但只能定义一个用户
#定义账号和密码 一旦自定义了账号和密码 原来自带的就不存在了 这里只能定义一个账号和密码
spring.security.user.nameadmin
spring.security.user.password1234565. 定义多用户–基于内存
定义一个配置类——springboot版本2.7.0以下
Configuration
public class MySecurityConfig extends WebSecurityConfigurerAdapter {//配置文件中的账号和密码失效Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.//基于内存完成认证和授权inMemoryAuthentication()// 表示用户名.withUser(lay)//表示密码.password(123456)//当前用户具有的角色.roles(admin)//表示具有的权限.authorities(user:select,user:delete,user:insert,user:update).and().withUser(xiumin).password(123456).roles(user).authorities(user:select,user:export);}输入密码登录后报错 没有使用密码加密器。 解决修改配置类——1.在配置类中添加密码加密器 Beanpublic PasswordEncoder passwordEncoder(){PasswordEncoder passwordEncodernew BCryptPasswordEncoder();return passwordEncoder;}修改配置类——给密码使用加密器
Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.inMemoryAuthentication().withUser(lay).password(passwordEncoder().encode(1007))//添加密码加密器.roles(admin).authorities(user:select).and().withUser(lays).password(passwordEncoder().encode(10072)).roles(user).authorities(user:export);}在password中添加密码加密器passwordEncoder().encode(密码) 6. 密码加密器 分成两种类型: 对称加密和非对称加密 对称加密:表示加密和解密使用同一把密钥。 非对称加密: 表示加密和解密不是使用同一个密钥。 md5 hash public class Test {public static void main(String[] args) {PasswordEncoder passwordEncodernew BCryptPasswordEncoder();//用于加密String encode passwordEncoder.encode(123456);String encode2 passwordEncoder.encode(123456);String encode3 passwordEncoder.encode(123456);System.out.println(encode);System.out.println(encode2);System.out.println(encode3);//安全.boolean matches passwordEncoder.matches(123456, encode2);System.out.println(是否密码正确:matches);}7. 获取当前用户的信息
// springsecurity默认把当前用户的信息保存SecurityContext上下文中.
GetMapping(info)public Authentication info(){//获取SecurityContext对象SecurityContext context SecurityContextHolder.getContext();//把用户得到信息封装到Authontication类中--用户名---角色以及权限---状态[]Authentication authentication context.getAuthentication();UserDetails principal (UserDetails) authentication.getPrincipal();// System.out.println(principal.getUsername());return authentication;}springsecurity默认为当前用户的信息保存在SecurityContext上下文中 首先获取SecurityContext对象 SecurityContext context SecurityContextHolder.getContext(); SecurityContextHolder:是安全上下文容器可以在此得知操作的用户是谁该用户是否已经被验证它拥有哪些角色权限这些都被保存在SecurityContextHolder中 返回认证信息——getAuthentication()方法 Authentication authentication context.getAuthentication(); 返回身份信息——getPrincipal()方法。UserDetail便是Spring对身份信息封装的一个接口 UserDetails principal (UserDetails) authentication.getPrincipal(); Authentication接口-认证信息包括 getAuthorities()权限信息列表默认是GrantedAuthority接口的一些实现类通常是代表权限信息的一系列字符串getCredentials()密码信息用户输入的密码字符串在认证过后通常会被移除用于保障安全getDetails()细节信息web应用中的实现接口通常为WebAuthenticationDetails,它记录了访问者的ip地址和sessionId的值getPrincipal()身份信息大部分情况下返回的是UserDetails接口的实现类也是框架中常用接口之一 8. security零散配置 Overrideprotected void configure(HttpSecurity http) throws Exception {http.formLogin()//登录页面;.loginPage(/login.html)//登录的处理路径 默认 /login.loginProcessingUrl(/login).successForwardUrl(/success) //登录成功转发的路径 必须为post请求.failureForwardUrl(/fail) //登录失败转发的路径 必须为post请求.permitAll(); //上面的请求路径无需认证http.csrf().disable();//禁用跨域伪造请求的过滤器//除了上的请求,其他请求都需要认证http.authorizeRequests().anyRequest().authenticated();}设置跳转到指定的登录页面——loginPage(/login.html)设置登录的处理路径——loginProcessingUrl(/login)【默认登录的处理路径为/login】设置成功转发的路径——successForwardUrl(/success)【必须为Post请求】设置登录失败转发的路径——failureForwardUrl(/fail)【必须为Post请求】表名上面的请求路径无需认证——permitAll()禁用跨域伪造请求的过滤器——http.csrf().disable();设置处理以上的请求其他请求都需要认证——http.authorizeRequests().anyRequest().authenticated(); 9. security完成授权 授权把当前用户具有的权限和对应的资源绑定的过程 授权一定发生在认证后 步骤
定义一些资源接口 GetMapping(select)public String select(){System.out.println(查询用户);return 查询用户;}GetMapping(insert)public String insert(){System.out.println(添加用户);return 添加用户;}GetMapping(update)public String update(){System.out.println(修改用户);return 修改用户;}GetMapping(delete)public String delete(){System.out.println(删除用户);return 删除用户;}GetMapping(export)public String export(){System.out.println(导出用户);return 导出用户;}修改配置类 在配置类中将资源和权限绑定
http.authorizeRequests().
//user/select资源与user:select权限绑定
antMatcher(/user/select).hasAuthority(user:select)
antMatcher(/user/insert).hasAuthority(user:insert)
antMatcher(/user/update).hasAuthority(user:update)
antMatcher(/user/delete).hasAuthority(user:delete)
antMatcher(/user/export).hasAuthority(user:export);10. 使用注解完成授权 上述的授权代码比较麻烦我们可以使用注解完成授权的过程 步骤 开启security授权的注解驱动 EnableGlobalMethodSecurity(prePostEnabled true)在资源上使用注解 PreAuthorize(hasAuthority(‘权限’)——资源执行前判断当前用户是否拥有该权限 GetMapping(select)PreAuthorize(hasAuthority(user:select)) //资源执行前判断当前用户是否拥有user:select权限public String select(){System.out.println(查询用户);return 查询用户;}修改配置类*——将配置类中上述的手动绑定的代码删除
11. 处理权限不足 权限不足使其跳转到指定页面 首先创建一个用于当权限不足时要跳转的页面 !DOCTYPE html
html langen
headmeta charsetUTF-8titleTitle/title
/head
body
权限不足 请联系管理员
/body
/html修改配置类 http.exceptionHandling().accessDeniedPage(/页面名称); //指定权限不足跳转的页面http.exceptionHandling().accessDeniedPage(/403.html); 文章转载自: http://www.morning.flmxl.cn.gov.cn.flmxl.cn http://www.morning.rhsr.cn.gov.cn.rhsr.cn http://www.morning.mzgq.cn.gov.cn.mzgq.cn http://www.morning.tlbhq.cn.gov.cn.tlbhq.cn http://www.morning.zsthg.cn.gov.cn.zsthg.cn http://www.morning.mjtgt.cn.gov.cn.mjtgt.cn http://www.morning.plqhb.cn.gov.cn.plqhb.cn http://www.morning.dblfl.cn.gov.cn.dblfl.cn http://www.morning.iterlog.com.gov.cn.iterlog.com http://www.morning.dblgm.cn.gov.cn.dblgm.cn http://www.morning.qfkxj.cn.gov.cn.qfkxj.cn http://www.morning.xbzfz.cn.gov.cn.xbzfz.cn http://www.morning.qnrpj.cn.gov.cn.qnrpj.cn http://www.morning.lhrwy.cn.gov.cn.lhrwy.cn http://www.morning.hqmfn.cn.gov.cn.hqmfn.cn http://www.morning.qxxj.cn.gov.cn.qxxj.cn http://www.morning.gqfjb.cn.gov.cn.gqfjb.cn http://www.morning.wmmqf.cn.gov.cn.wmmqf.cn http://www.morning.fqqcd.cn.gov.cn.fqqcd.cn http://www.morning.brzlp.cn.gov.cn.brzlp.cn http://www.morning.zcyxq.cn.gov.cn.zcyxq.cn http://www.morning.fpngg.cn.gov.cn.fpngg.cn http://www.morning.zrpbf.cn.gov.cn.zrpbf.cn http://www.morning.fbjqq.cn.gov.cn.fbjqq.cn http://www.morning.lzzqz.cn.gov.cn.lzzqz.cn http://www.morning.cwwts.cn.gov.cn.cwwts.cn http://www.morning.fzlk.cn.gov.cn.fzlk.cn http://www.morning.yrmpr.cn.gov.cn.yrmpr.cn http://www.morning.qjdqj.cn.gov.cn.qjdqj.cn http://www.morning.flxqm.cn.gov.cn.flxqm.cn http://www.morning.bqwnp.cn.gov.cn.bqwnp.cn http://www.morning.qzxb.cn.gov.cn.qzxb.cn http://www.morning.ho-use.cn.gov.cn.ho-use.cn http://www.morning.lhxkl.cn.gov.cn.lhxkl.cn http://www.morning.xpqyf.cn.gov.cn.xpqyf.cn http://www.morning.zlwg.cn.gov.cn.zlwg.cn http://www.morning.rltsx.cn.gov.cn.rltsx.cn http://www.morning.plqqn.cn.gov.cn.plqqn.cn http://www.morning.xltwg.cn.gov.cn.xltwg.cn http://www.morning.jgzmr.cn.gov.cn.jgzmr.cn http://www.morning.zcnfm.cn.gov.cn.zcnfm.cn http://www.morning.nnjq.cn.gov.cn.nnjq.cn http://www.morning.nclbk.cn.gov.cn.nclbk.cn http://www.morning.kmcfw.cn.gov.cn.kmcfw.cn http://www.morning.gyzfp.cn.gov.cn.gyzfp.cn http://www.morning.tnkwj.cn.gov.cn.tnkwj.cn http://www.morning.jmlgk.cn.gov.cn.jmlgk.cn http://www.morning.rmppf.cn.gov.cn.rmppf.cn http://www.morning.rdwm.cn.gov.cn.rdwm.cn http://www.morning.pwmpn.cn.gov.cn.pwmpn.cn http://www.morning.bhqlj.cn.gov.cn.bhqlj.cn http://www.morning.glxmf.cn.gov.cn.glxmf.cn http://www.morning.mygbt.cn.gov.cn.mygbt.cn http://www.morning.csdgt.cn.gov.cn.csdgt.cn http://www.morning.qxlyf.cn.gov.cn.qxlyf.cn http://www.morning.rfyk.cn.gov.cn.rfyk.cn http://www.morning.fwkjp.cn.gov.cn.fwkjp.cn http://www.morning.rpsjh.cn.gov.cn.rpsjh.cn http://www.morning.mhcys.cn.gov.cn.mhcys.cn http://www.morning.ryrpq.cn.gov.cn.ryrpq.cn http://www.morning.pwrkl.cn.gov.cn.pwrkl.cn http://www.morning.rhwty.cn.gov.cn.rhwty.cn http://www.morning.wlfxn.cn.gov.cn.wlfxn.cn http://www.morning.rhqn.cn.gov.cn.rhqn.cn http://www.morning.wjzzh.cn.gov.cn.wjzzh.cn http://www.morning.qxxj.cn.gov.cn.qxxj.cn http://www.morning.swlwf.cn.gov.cn.swlwf.cn http://www.morning.schwr.cn.gov.cn.schwr.cn http://www.morning.qdcpn.cn.gov.cn.qdcpn.cn http://www.morning.pbbzn.cn.gov.cn.pbbzn.cn http://www.morning.brtxg.cn.gov.cn.brtxg.cn http://www.morning.rfycj.cn.gov.cn.rfycj.cn http://www.morning.gqksd.cn.gov.cn.gqksd.cn http://www.morning.wdpt.cn.gov.cn.wdpt.cn http://www.morning.ctwwq.cn.gov.cn.ctwwq.cn http://www.morning.rnhh.cn.gov.cn.rnhh.cn http://www.morning.wztlr.cn.gov.cn.wztlr.cn http://www.morning.jsljr.cn.gov.cn.jsljr.cn http://www.morning.bxfy.cn.gov.cn.bxfy.cn http://www.morning.wscfl.cn.gov.cn.wscfl.cn