当前位置: 首页 > news >正文

购物网站开发的难点网络平台推广服务

购物网站开发的难点,网络平台推广服务,个人网站名,phpwind做的网站集成SpringSecurity 安全简介 在 Web 开发中#xff0c;安全一直是非常重要的一个方面。安全虽然属于应用的非功能性需求#xff0c;但是应该在应用开发的初期就考虑进来。如果在应用开发的后期才考虑安全的问题#xff0c;就可能陷入一个两难的境地#xff1a;一方面安全一直是非常重要的一个方面。安全虽然属于应用的非功能性需求但是应该在应用开发的初期就考虑进来。如果在应用开发的后期才考虑安全的问题就可能陷入一个两难的境地一方面应用存在严重的安全漏洞无法满足用户的要求并可能造成用户的隐私数据被攻击者窃取另一方面应用的基本架构已经确定要修复安全漏洞可能需要对系统的架构做出比较重大的调整因而需要更多的开发时间影响应用的发布进程。因此从应用开发的第一天就应该把安全相关的因素考虑进来并在整个应用的开发过程中。 市面上存在比较有名的ShiroSpring Security 这里需要阐述一下的是每一个框架的出现都是为了解决某一问题而产生了那么Spring Security框架的出现是为了解决什么问题呢 首先我们看下它的官网介绍Spring Security官网地址 Spring Security is a powerful and highly customizable authentication and access-control framework. It is the de-facto standard for securing Spring-based applications. Spring Security is a framework that focuses on providing both authentication and authorization to Java applications. Like all Spring projects, the real power of Spring Security is found in how easily it can be extended to meet custom requirements Spring Security是一个功能强大且高度可定制的身份验证和访问控制框架。它实际上是保护基于spring的应用程序的标准。 Spring Security是一个框架侧重于为Java应用程序提供身份验证和授权。与所有Spring项目一样Spring安全性的真正强大之处在于它可以轻松地扩展以满足定制需求 从官网的介绍中可以知道这是一个权限框架。想我们之前做项目是没有使用框架是怎么控制权限的对于权限 一般会细分为功能权限访问权限和菜单权限。代码会写的非常的繁琐冗余。 怎么解决之前写权限代码繁琐冗余的问题一些主流框架就应运而生而Spring Scecurity就是其中的一种。 Spring 是一个非常流行和成功的 Java 应用开发框架。Spring Security 基于 Spring 框架提供了一套 Web 应用安全性的完整解决方案。一般来说Web 应用的安全性包括用户认证Authentication和用户授权Authorization两个部分。用户认证指的是验证某个用户是否为系统中的合法主体也就是说用户能否访问该系统。用户认证一般要求用户提供用户名和密码。系统通过校验用户名和密码来完成认证过程。用户授权指的是验证某个用户是否有权限执行某个操作。在一个系统中不同用户所具有的权限是不同的。比如对一个文件来说有的用户只能进行读取而有的用户可以进行修改。一般来说系统会为不同的用户分配不同的角色而每个角色则对应一系列的权限。 对于上面提到的两种应用情景Spring Security 框架都有很好的支持。在用户认证方面Spring Security 框架支持主流的认证方式包括 HTTP 基本认证、HTTP 表单验证、HTTP 摘要认证、OpenID 和 LDAP 等。在用户授权方面Spring Security 提供了基于角色的访问控制和访问控制列表Access Control ListACL可以对应用中的领域对象进行细粒度的控制。 实战测试 环境搭建 1、新建一个初始的springboot项目web模块thymeleaf模块(导包) dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-thymeleaf/artifactId/dependency2、导入静态资源 welcome.html |views |level1 1.html 2.html 3.html |level2 1.html 2.html 3.html |level3 1.html 2.html 3.html Login.html3、controller跳转 package com.kuang.controller;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping;Controller public class RouterController {RequestMapping({/,/index})public String index(){return index;}RequestMapping(/toLogin)public String toLogin(){return views/login;}RequestMapping(/level1/{id})public String level1(PathVariable(id) int id){return views/level1/id;}RequestMapping(/level2/{id})public String level2(PathVariable(id) int id){return views/level2/id;}RequestMapping(/level3/{id})public String level3(PathVariable(id) int id){return views/level3/id;}}4、测试实验环境是否OK 认识SpringSecurity Spring Security 是针对Spring项目的安全框架也是Spring Boot底层安全模块默认的技术选型他可以实现强大的Web安全控制对于安全控制我们仅需要引入 spring-boot-starter-security 模块进行少量的配置即可实现强大的安全管理 记住几个类 WebSecurityConfigurerAdapter自定义Security策略AuthenticationManagerBuilder自定义认证策略EnableWebSecurity开启WebSecurity模式 EnableXXX开启某个功能 Spring Security的两个主要目标是 “认证” 和 “授权”访问控制。 “认证”Authentication 身份验证是关于验证您的凭据如用户名/用户ID和密码以验证您的身份。 身份验证通常通过用户名和密码完成有时与身份验证因素结合使用。 “授权” Authorization 授权发生在系统成功验证您的身份后最终会授予您访问资源如信息文件数据库资金位置几乎任何内容的完全权限。 这个概念是通用的而不是只在Spring Security 中存在。 认证和授权 目前我们的测试环境是谁都可以访问的我们使用 Spring Security 增加上认证和授权的功能 1、引入 Spring Security 模块 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-security/artifactId /dependency2、编写 Spring Security 配置类 参考官网https://spring.io/projects/spring-security 查看我们自己项目中的版本找到对应的帮助文档 https://docs.spring.io/spring-security/site/docs/5.3.0.RELEASE/reference/html5 3、编写基础配置类 package com.kuang.config;import org.springframework.security.config.annotation.web.builders.HttpSecurity; importorg.springframework.security.config.annotation.web.configuration.EnableWebSecurity; importorg.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;EnableWebSecurity // 开启WebSecurity模式 public class SecurityConfig extends WebSecurityConfigurerAdapter {Overrideprotected void configure(HttpSecurity http) throws Exception {} }4、定制请求的授权规则 Override protected void configure(HttpSecurity http) throws Exception {// 定制请求的授权规则// 首页所有人可以访问http.authorizeRequests().antMatchers(/).permitAll().antMatchers(/level1/**).hasRole(vip1).antMatchers(/level2/**).hasRole(vip2).antMatchers(/level3/**).hasRole(vip3); }5、测试一下发现除了首页都进不去了因为我们目前没有登录的角色因为请求需要登录的角色拥有对应的权限才可以 6、在configure()方法中加入以下配置开启自动配置的登录功能 // 开启自动配置的登录功能 // /login 请求来到登录页 // /login?error 重定向到这里表示登录失败 http.formLogin();7、测试一下发现没有权限的时候会跳转到登录的页面 memory 8、查看刚才登录页的注释信息 我们可以定义认证规则重写configure(AuthenticationManagerBuilder auth)方法 //定义认证规则 Override protected void configure(AuthenticationManagerBuilder auth) throws Exception {//在内存中定义也可以在jdbc中去拿....auth.inMemoryAuthentication().withUser(kuangshen).password(123456).roles(vip2,vip3).and().withUser(root).password(123456).roles(vip1,vip2,vip3).and().withUser(guest).password(123456).roles(vip1,vip2); }9、测试我们可以使用这些账号登录进行测试发现会报错 There is no PasswordEncoder mapped for the id “null” [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lEaXb88n-1610169168519)(data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg)] 10、原因我们要将前端传过来的密码进行某种方式加密否则就无法登录修改代码 //定义认证规则 Override protected void configure(AuthenticationManagerBuilder auth) throws Exception {//在内存中定义也可以在jdbc中去拿....//Spring security 5.0中新增了多种加密方式也改变了密码的格式。//要想我们的项目还能够正常登陆需要修改一下configure中的代码。我们要将前端传过来的密码进行某种方式加密//spring security 官方推荐的是使用bcrypt加密方式。auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser(kuangshen).password(newBCryptPasswordEncoder().encode(123456)).roles(vip2,vip3).and().withUser(root).password(newBCryptPasswordEncoder().encode(123456)).roles(vip1,vip2,vip3).and().withUser(guest).password(newBCryptPasswordEncoder().encode(123456)).roles(vip1,vip2); }11、测试发现登录成功并且每个角色只能访问自己认证下的规则搞定 数据库 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7eDReTSJ-1610169168522)(C:\Users\王东梁\AppData\Roaming\Typora\typora-user-images\image-20210109001124828.png)] 权限控制和注销 1、开启自动配置的注销的功能 //定制请求的授权规则 Override protected void configure(HttpSecurity http) throws Exception {//....//开启自动配置的注销的功能// /logout 注销请求http.logout(); }2、我们在前端增加一个注销的按钮index.html 导航栏中 a classitem th:href{/logout}i classaddress card icon/i 注销 /a3、我们可以去测试一下登录成功后点击注销发现注销完毕会跳转到登录页面 4、但是我们想让他注销成功后依旧可以跳转到首页该怎么处理呢 // .logoutSuccessUrl(/); 注销成功来到首页 http.logout().logoutSuccessUrl(/);5、测试注销完毕后发现跳转到首页OK 6、我们现在又来一个需求用户没有登录的时候导航栏上只显示登录按钮用户登录之后导航栏可以显示登录的用户信息及注销按钮还有就是比如kuangshen这个用户它只有 vip2vip3功能那么登录则只显示这两个功能而vip1的功能菜单不显示这个就是真实的网站情况了该如何做呢 我们需要结合thymeleaf中的一些功能 secauthorize“isAuthenticated()”:是否认证登录来显示不同的页面 Maven依赖 !-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity4 -- dependencygroupIdorg.thymeleaf.extras/groupIdartifactIdthymeleaf-extras-springsecurity5/artifactIdversion3.0.4.RELEASE/version /dependency7、修改我们的 前端页面 导入命名空间 xmlns:sechttp://www.thymeleaf.org/thymeleaf-extras-springsecurity5修改导航栏增加认证判断 !--登录注销-- div classright menu!--如果未登录--div sec:authorize!isAuthenticated()a classitem th:href{/login}i classaddress card icon/i 登录/a/div!--如果已登录--div sec:authorizeisAuthenticated()a classitemi classaddress card icon/i用户名span sec:authenticationprincipal.username/span角色span sec:authenticationprincipal.authorities/span/a/divdiv sec:authorizeisAuthenticated()a classitem th:href{/logout}i classaddress card icon/i 注销/a/div /div8、重启测试我们可以登录试试看登录成功后确实显示了我们想要的页面 9、如果注销404了就是因为它默认防止csrf跨站请求伪造因为会产生安全问题我们可以将请求改为post表单提交或者在spring security中关闭csrf功能我们试试在 配置中增加 http.csrf().disable(); http.csrf().disable();//关闭csrf功能:跨站请求伪造,默认只能通过post方式提交logout请求 http.logout().logoutSuccessUrl(/);10、我们继续将下面的角色功能块认证完成 !-- sec:authorizehasRole(vip1) -- div classcolumn sec:authorizehasRole(vip1)div classui raised segmentdiv classuidiv classcontenth5 classcontentLevel 1/h5hrdiva th:href{/level1/1}i classbullhorn icon/i Level-1-1/a/divdiva th:href{/level1/2}i classbullhorn icon/i Level-1-2/a/divdiva th:href{/level1/3}i classbullhorn icon/i Level-1-3/a/div/div/div/div /divdiv classcolumn sec:authorizehasRole(vip2)div classui raised segmentdiv classuidiv classcontenth5 classcontentLevel 2/h5hrdiva th:href{/level2/1}i classbullhorn icon/i Level-2-1/a/divdiva th:href{/level2/2}i classbullhorn icon/i Level-2-2/a/divdiva th:href{/level2/3}i classbullhorn icon/i Level-2-3/a/div/div/div/div /divdiv classcolumn sec:authorizehasRole(vip3)div classui raised segmentdiv classuidiv classcontenth5 classcontentLevel 3/h5hrdiva th:href{/level3/1}i classbullhorn icon/i Level-3-1/a/divdiva th:href{/level3/2}i classbullhorn icon/i Level-3-2/a/divdiva th:href{/level3/3}i classbullhorn icon/i Level-3-3/a/div/div/div/div /div11、测试一下 12、权限控制和注销搞定 记住我 现在的情况我们只要登录之后关闭浏览器再登录就会让我们重新登录但是很多网站的情况就是有一个记住密码的功能这个该如何实现呢很简单 1、开启记住我功能 //定制请求的授权规则 Override protected void configure(HttpSecurity http) throws Exception { //。。。。。。。。。。。//记住我http.rememberMe(); }2、我们再次启动项目测试一下发现登录页多了一个记住我功能我们登录之后关闭 浏览器然后重新打开浏览器访问发现用户依旧存在 思考如何实现的呢其实非常简单 我们可以查看浏览器的cookie 3、我们点击注销的时候可以发现spring security 帮我们自动删除了这个 cookie [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bxJ2PHVE-1610169168526)(data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg)]4、结论登录成功后将cookie发送给浏览器保存以后登录带上这个cookie只要通过检查就可以免登录了。如果点击注销则会删除这个cookie具体的原理我们在JavaWeb阶段都讲过了. 定制登录页 现在这个登录页面都是spring security 默认的怎么样可以使用我们自己写的Login界面呢 1、在刚才的登录页配置后面指定 loginpage http.formLogin().loginPage(/toLogin);2、然后前端也需要指向我们自己定义的 login请求 a classitem th:href{/toLogin}i classaddress card icon/i 登录 /a3、我们登录需要将这些信息发送到哪里我们也需要配置login.html 配置提交请求及方式方式必须为post: 在 loginPage()源码中的注释上有写明 form th:action{/login} methodpostdiv classfieldlabelUsername/labeldiv classui left icon inputinput typetext placeholderUsername nameusernamei classuser icon/i/div/divdiv classfieldlabelPassword/labeldiv classui left icon inputinput typepassword namepasswordi classlock icon/i/div/divinput typesubmit classui blue submit button/ /form4、这个请求提交上来我们还需要验证处理怎么做呢我们可以查看formLogin()方法的源码我们配置接收登录的用户名和密码的参数 http.formLogin().usernameParameter(username).passwordParameter(password).loginPage(/toLogin).loginProcessingUrl(/login); // 登陆表单提交请求5、在登录页增加记住我的多选框 input typecheckbox nameremember 记住我6、后端验证处理 //定制记住我的参数 http.rememberMe().rememberMeParameter(remember);7、测试OK 完整配置代码 package com.kuang.config;importorg.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; importorg.springframework.security.config.annotation.web.configuration.EnableWebSecurity; importorg.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter {//定制请求的授权规则Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers(/).permitAll().antMatchers(/level1/**).hasRole(vip1).antMatchers(/level2/**).hasRole(vip2).antMatchers(/level3/**).hasRole(vip3);//开启自动配置的登录功能如果没有权限就会跳转到登录页面// /login 请求来到登录页// /login?error 重定向到这里表示登录失败http.formLogin().usernameParameter(username).passwordParameter(password).loginPage(/toLogin).loginProcessingUrl(/login); // 登陆表单提交请求//开启自动配置的注销的功能// /logout 注销请求// .logoutSuccessUrl(/); 注销成功来到首页http.csrf().disable();//关闭csrf功能:跨站请求伪造,默认只能通过post方式提交logout请求http.logout().logoutSuccessUrl(/);//记住我http.rememberMe().rememberMeParameter(remember);}//定义认证规则Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {//在内存中定义也可以在jdbc中去拿....//Spring security 5.0中新增了多种加密方式也改变了密码的格式。//要想我们的项目还能够正常登陆需要修改一下configure中的代码。我们要将前端传过来的密码进行某种方式加密//spring security 官方推荐的是使用bcrypt加密方式。auth.inMemoryAuthentication().passwordEncoder(newBCryptPasswordEncoder()).withUser(kuangshen).password(newBCryptPasswordEncoder().encode(123456)).roles(vip2,vip3).and().withUser(root).password(newBCryptPasswordEncoder().encode(123456)).roles(vip1,vip2,vip3).and().withUser(guest).password(newBCryptPasswordEncoder().encode(123456)).roles(vip1,vip2);} }
http://www.huolong8.cn/news/431383/

相关文章:

  • 网站竞价建一个网站迈年
  • 服装公司网站策划书网站编辑转行做文案
  • 欧亚达网站是哪家公司做的app下载网站建设
  • 长景园林这个网站谁做的软件设计开发
  • 永康网站优化公司昆明做百度网站电话号码
  • 杭州建设企业网站的2023年楼市将迎来抛售潮
  • 忘记网站后台admin密码自助建网站不需要域名
  • 网站学什么版面设计的概念
  • iis7搭建网站织梦软件开发文档的需求分析
  • 网站建设实训报告册微信分销app
  • 滁州网站建设哪个好点成都住建局官网平台登录
  • 怎么制作钓鱼网站建一个购物网站多少钱
  • 哪个网站教做pptwordpress 酒店中文主题
  • 百度收录网站提交入口做网站学什么语言
  • 建站公司 网络服务电商商城网站开发
  • 做医疗信息网站的域名网站做生鲜线下推广建议
  • 一站传媒seo优化在百度上怎么做网站
  • 什么网站可以免费做护师题南京 网站备案
  • 校园网站建设系统设计合肥网站建设+一浪
  • 做网店好还是网站好广告公司简介宣传册
  • 网站域名需要备案吗网站开发无使用期限怎么摊销
  • 深圳做微商网站的公司广州公共资源建设工程交易中心网站
  • 郑州做网站的专业公司有哪些福建建设工程设计备案网站
  • 网页编辑代码模板seo网站关键词排名软件
  • 在建设银行网站上还贷郑州官方网
  • 电子商务网站建设与管理的实验报告网站开发 外包 哪家
  • 河南seo网站多少钱win7优化教程
  • 网站建设培训深圳杭州seo排名费用
  • 国家职业建设中心网站centos wordpress 安装
  • 网站开发需求确认书vis设计