帮别人做网站赚钱吗,中山快速建站合作,深圳做电商网站,南昌网站建设渠道本篇为SpringSecurity的第一篇#xff0c;主要来介绍下什么是SpringSecurity#xff0c;以及在springboot中如何使用它I. 基本知识点官方文档: https://docs.spring.io/spring-security/site/docs/5.2.2.BUILD-SNAPSHOT/reference/htmlsingle/#community-help下面是官方介绍S…本篇为SpringSecurity的第一篇主要来介绍下什么是SpringSecurity以及在springboot中如何使用它I. 基本知识点官方文档: https://docs.spring.io/spring-security/site/docs/5.2.2.BUILD-SNAPSHOT/reference/htmlsingle/#community-help下面是官方介绍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用国语简单抽象的说一下它的定义很的认证和访问权限校验框架那么具体能干嘛用户登录认证用户名密码登录确定用户身份用户访问鉴权(常见的ACL访问控制列表RBAC角色访问控制)判定是否有权限访问某个资源安全保护(CSRF跨站点攻击,Session Fixation会话固定攻击...)II. 初体验接下来我们看一下再springboot中如何使用springsecurity1. 配置首先得是spring boot项目然后添加上security的依赖即可相对完整的pom配置如下(注意我们使用的springboot版本为2.2.1.RELEASE)org.springframework.boot spring-boot-starter-parent 2.2.1.RELEASEUTF-8UTF-81.8org.springframework.boot spring-boot-starter-security org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-maven-plugin spring-snapshotsSpring Snapshotshttps://repo.spring.io/libs-snapshot-localtruespring-milestonesSpring Milestoneshttps://repo.spring.io/libs-milestone-localfalsespring-releasesSpring Releaseshttps://repo.spring.io/libs-release-localfalse2. 实例demo上面配置完之后啥都不需要干项目已经接入了spring security项目中的服务都需要登录之后才能访问// 程序启动类SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}// rest 服务RestControllerpublic class IndexRest { GetMapping(path {/, /index}) public String index() { return hello this is index!; } GetMapping(path hello) public String hello(String name) { return welcome name; }}当我们需要访问首页时会发现直接302重定向到登录页面了如下图spring security默认给我们生成了一个用户名为user密码为控制台中输出的一行日志如Using generated security password: aa410186-5c04-4282-b217-507ffb1f61eb登录之后会重定向回我们之前访问的url通过抓包可以看到登录成功之后会设置请求方的cookie后续的请求携带cookie来表明用户身份3. 基本配置上面虽然演示了一个hello world的初体验项目但是这个默认的用户名/密码有点鬼畜默认的配置主要来自于org.springframework.boot.autoconfigure.security.SecurityProperties.User下面是截图(所以前面的用户名为user)接下来我们需要配置为对人类友好的方式在项目的配置文件application.yml中指定登录的用户名/密码spring: security: user: name: yihuihui password: 123456重启测试项目使用新的用户名/密码(yihuihui/123456)就可以登录成功了;4. 用户身份获取上面虽然是一个简单的case但还有一点不得不提一下在我的接口中虽然知道你登录了但怎么知道你是谁呢我们可以直接通过HttpServletRequest#getRemoteUser()的方法来获取登录用户 或者通过SecurityContextHolder.getContext().getAuthentication().getPrincipal()来获取授权信息我们来写一个通用方法public String getUser() { return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getRemoteUser();}// orpublic Object getUser() { SecurityContextHolder.getContext().getAuthentication().getPrincipal();}然后稍微改一下我们的服务接口GetMapping(path {/, /index})public String index() { return hello this is index! welcome getUser();}再次访问之后结果如下5. 小结本文主要是spring security系列的起源篇第一节介绍了下什么是SpringSecurity有什么特点spring security是一个很的认证(可以简单理解为登录验证)和鉴权(可简单理解为访问控制)框架三大特点登录 鉴权 安全防护第二节介绍了一个简单入门的HelloWorld实例springboot项目添加依赖 spring-boot-starter-security 所有的http接口访问都需要登录默认提供一个用户名为user密码为控制台输出的UUID字符串通过spring.security.user.name和spring.security.user.password来指定用户名密码通过HttpServletRequest#getRemoteUser()获取登录用户那么问题来了什么系统可能只有一个用户呢要多用户怎么办不同的用户不同的权限怎么办某些接口所有人都可以访问又怎么办II. 其他0. 项目工程https://github.com/liuyueyi/spring-boot-demo代码: https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-security/000-basic-demo1. 一灰灰Blog尽信书则不如以上内容纯属一家之言因个人能力有限难免有疏漏和错误之处如发现bug或者有更好的建议欢迎批评指正不吝感激下面一灰灰的个人博客记录所有学习和工作中的博文欢迎大家前去逛逛一灰灰Blog个人博客 https://blog.hhui.top一灰灰Blog-Spring专题博客 http://spring.hhui.top