2019独角兽企业重金招聘Python工程师标准>>>

1.关于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 Security是一个功能强大且高度可定制的用于认证(authentication)和访问控制(access-control)的框架。该框架致力于为java应用提供认证(authentication)和授权(authorization),并且非常容易根据需要进行扩展,总之就是说Spring Security很diao啦。

2.在Spring boot中的基本使用

1)继承WebSecurityConfigurerAdapter,并重写configure(AuthenticationManagerBuilder auth) 和 configure(HttpSecurity http)方法。

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Autowiredprivate AuthUserService authUserService;@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.userDetailsService(authUserService).passwordEncoder(new BCryptPasswordEncoder());}@Overrideprotected void configure(HttpSecurity http) throws Exception {//定义/login  /upload不需要登录//定义登录页面为/loginhttp.authorizeRequests().antMatchers("/login","/register").permitAll().antMatchers("/css/**","/js/**").permitAll().anyRequest().authenticated().and().formLogin().loginPage("/login").successForwardUrl("/").permitAll().and().logout().permitAll();}
}

@EnableWebSecurity和@Configuration注解表名了这是一个Spring Security配置类,有了这两个注解,Spring Security会自动去识别加载。

configure(HttpSecurity http)方法定义了哪些地方需要权限控制以及定义了自定义登录页(否则就会加载Spring Security的自带登录页)。

configure(AuthenticationManagerBuilder auth) 方法定义了通过mybatis方式实现登录认证,以及规定了加密方法为:BCrypt。AuthenticationManagerBuilder 用于创建一个 AuthenticationManager,让我们能够轻松的实现内存验证、LADP验证、基于JDBC的验证、添加 UserDetailsService、添加 AuthenticationProvider

其中AuthUserService如下:

@Service
public class AuthUserService implements UserDetailsService {@Autowiredprivate UserService userService;@Overridepublic UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {var user=userService.login(s);if(user==null){throw new UsernameNotFoundException("User "+s+" not found.");}System.out.println(user.toString());List<SimpleGrantedAuthority> simpleGrantedAuthorities=new ArrayList<>();simpleGrantedAuthorities.add(new SimpleGrantedAuthority("USERADMIN"));return new User(user.getAccount(),user.getPassword(),simpleGrantedAuthorities);}
}

UserService是调用mybatis方法获取user。

UserDetailsService是Spring Security内置的用户信息接口,实现此接口的loadUserByUsername方法后,和上面的SecurityConfig类结合起来,Spring Security会自动去验证账号密码,认证通过后就会跳转到预设的地址。

3.Spring Security认证原理

参考:Spring4all:Spring-Security-入门

转载于:https://my.oschina.net/yunduansing/blog/2032475

Spring boot 2.x+oauth2实现单点登录:基础准备之Spring Security相关推荐

  1. (附源码)Spring Boot 框架整合 OAuth2 实现单点登录 SSO 详细完整源码教程!

    1.  前言 技术这东西吧,看别人写的好像很简单似的,到自己去写的时候就各种问题,"一看就会,一做就错".网上关于实现SSO的文章一大堆,但是当你真的照着写的时候就会发现根本不是那 ...

  2. Spring Security OAuth2 SSO 单点登录

    基于 Spring Security OAuth2 SSO 单点登录系统 SSO简介 单点登录(英语:Single sign-on,缩写为 SSO),又译为单一签入,一种对于许多相互关连,但是又是各自 ...

  3. OAuth2 实现单点登录 SSO

    转载自  OAuth2 实现单点登录 SSO 1. 前言 技术这东西吧,看别人写的好像很简单似的,到自己去写的时候就各种问题,"一看就会,一做就错".网上关于实现SSO的文章一大堆 ...

  4. OAuth2实现单点登录SSO

    本文转载自:https://www.cnblogs.com/cjsblog/p/10548022.html OAuth2实现单点登录SSO 1.  前言 技术这东西吧,看别人写的好像很简单似的,到自己 ...

  5. Java开发 - 单点登录初体验(Spring Security + JWT)

    目录​​​​​​​ 前言 为什么要登录 登录的种类 Cookie-Session Cookie-Session-local storage JWT令牌 几种登陆总结 用户身份认证与授权 创建工程 添加 ...

  6. OAuth2实现单点登录SSO完整教程,其实不难!

    我是猿人,一个热爱技术.热爱编程的IT猿.技术是开源的,知识是共享的! 写作是对自己学习的总结和记录,如果您对 Java.分布式.微服务.中间件.Spring Boot.Spring Cloud等技术 ...

  7. 单点登录(shiro与Spring Security OAuth 2.0的集成)

    单点登录(shiro与Spring Security OAuth 2.0的集成) shiro项目采用ruoyi,OAuth采用pig 若依:https://gitee.com/y_project/Ru ...

  8. 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】

    [原创文章,转载请注明出处] 个人使用比较习惯的json框架是fastjson,所以spring boot默认的json使用起来就很陌生了,所以很自然我就想我能不能使用fastjson进行json解析 ...

  9. Spring Boot+Vue前后端分离商城实战(二)Spring Boot 项目构建及使用

    在介绍了基础环境搭建之后,这一篇文章将介绍如何使用 IDEA 进行 Spring Boot 项目的开发和调试,希望大家能够尽快上手和体验 Spring Boot 项目开发.关于 Spring Boot ...

  10. 【Spring Boot】Spring Boot @EnableOAuth2Sso | 启用 OAuth2 单点登录

    文章目录 演示工具版本 Maven 依赖 使用 @EnableOAuth2Sso OAuth2 配置 登出 完整示例 输出 参考文献 源码下载 本页将介绍Spring Security OAuth2 ...

最新文章

  1. 容器监控实践—Heapster
  2. 2021第六届数维杯大学生数学建模竞赛赛题_B 中小城市地铁运营与建设优化设计
  3. 云服务器 VNC 远程连接
  4. 指针的理解 -- java程序员学C语言日记二
  5. Selenium系列文章汇总
  6. em模型补缺失值_EM算法学习(三)
  7. xheditor可视化富文本编辑器
  8. iOS App 签名的原理 App 重签名(二)
  9. 屌炸天的3D引擎OpenCASCADE的用法及案例
  10. python怎么画圆并改变线条颜色_python – 更改plot_surface中的线条颜色
  11. 短视频行业深度(一)快手科技:私域筑垒,公域进军.PDF
  12. 阿里云盘PC端内测版可以下载安装了 还送你800G扩容福利码!
  13. 淘宝API item_password - 获得淘口令真实url
  14. 微信小程序实现列表项左滑删除效果
  15. 创意发明:基于stm32的微型掌上示波器 设计说明书电路及源代码
  16. java将图片url转换成数据流输出到前端
  17. 接口测试平台代码实现86: 全局请求头-1
  18. 基于线段的激光雷达单目曲面重建(ICRA2021)
  19. 视频转换中的码率是什么意思?它的大小有什么作用?
  20. 前端开发_HTML5_布局-栅格布局

热门文章

  1. LVM扩容之xfs文件系统
  2. ios开发网络学习AFN框架的使用一:get和post请求
  3. Python学习笔记008_类_对象_继承_组合_类相关的BIF
  4. 50多条mysql数据库优化建议
  5. linux shell 随机字符生成单词
  6. RegularExpressions
  7. 给自定义tabBar的按钮添加点击放大缩小的动画
  8. 线性渐变和放射性渐变
  9. java ssh 框架下 利用junit4 spring-test进行单元测试
  10. Foundation框架: 6.NSString的创建和导出