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. 基本函数依赖和候选键_5G 通信候选波形的设计和评测解决方案
  2. Flask的闪现(message) 请求扩展 中间件 蓝图
  3. C++ Primer 5th笔记(chap 14 重载运算和类型转换)算术和关系运算符
  4. python(numpy,pandas12)——pandas plot 画图
  5. 如何判断两个jq对象是同一个对象
  6. vue 左侧菜单隐藏_vue.js 左侧二级菜单显示与隐藏切换的实例代码
  7. AppCompatActivity中使用SearchView
  8. SQL 查询CET使用领悟
  9. esp8266一键安装arduino板_STM32 与 Arduino
  10. 上证指数ARIMA模型预测(R软件)
  11. Java前景如何,容易找工作嘛
  12. 删库跑路 php,程序员删库跑路事件,php中文网的几点声明!
  13. https配置CA证书安装教程
  14. 武汉大学计算机学院夏桂松,实验室硕士研究生王碧杉、何敬伟在ECCV上发表事件相机高质量图像重建论文...
  15. Attention中的Q、K、V
  16. PCB图纸太小元件放不下怎么办
  17. TVS(瞬态抑制二极管)、Schottky(肖特基二极管)、Zener (齐纳二极管,也称稳压二极管)主要特点及区别和使用
  18. 项目管理四个核心要素
  19. 任正非在持股员工代表会上讲:我的家人永不会进入接班人序列
  20. java 初级、中级、高级工程师

热门文章

  1. 使用css的类名交集复合选择器 《转》
  2. Flask 蓝图(Blueprint)
  3. 微信运营:必须收藏的101条万能微信标题公式
  4. 有用的linux命令笔记
  5. [Python]关键字is和操作符==
  6. 剑指:合并两个排序的链表
  7. 201803101101_《函数的原始写法》
  8. Linq-查询上一条下一条
  9. Zoj 3511 线段树
  10. 下面的代码能确定参数是否为奇数吗?