Spring Security 学习+实践
Spring Security是Spring为解决应用安全所提供的一个全面的安全性解决方案。基于Spring AOP和Servlet过滤器,启动时在Spring上下文中注入了一组安全应用的Bean,并在应用开发中提供了声明式的安全访问控制功能,使开发者可以在请求级和方法级上处理用户身份认证与鉴权,大大减少了应用开发安全处理时编写代码的工作量。
接下来通过代码来实践一番,首先在pom中添加security的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
添加依赖后启动服务,可以看到控制台打印如下信息:

当Spring Security启动时,如果没有指定用户服务则会创建一个默认的用户,登录名称为user,5392bb9d-0673-4a9f-ab1a-c07522c84c5f是登录口令。
这时如果我们通过postman去请求,会返回如下结果:

按照默认的认证方式传递用户名和密码即可成功请求获取返回值,如下图所示:

如果通过浏览器访问,则会自动跳转到默认的登录页面:

通过上述测试,我们对Spring Security能实现的效果有了一个直观的感受。但是在实际应用场景中,认证的方式是需要开发人员自己来实现的,那么如何让Spring Security使用我们所定义的用户名和登录口令呢?
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter { @Bean
@Override
public UserDetailsService userDetailsServiceBean() throws Exception {
return super.userDetailsServiceBean();
} @Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
} @Override
protected void configure(AuthenticationManagerBuilder authManagerBuilder) throws Exception {
authManagerBuilder.inMemoryAuthentication().withUser("eddy").password("123456").roles("user");
}
}
在上面的代码实现中继承了WebSecurityConfigurerAdapter并使用其所提供的默认配置,通过覆写configure()方法,以内存的方式增加应用用户信息的定义。在增加的用户中我们设置了用户的登录名、登录口令及相应的用户权限(角色)。
这时我们重新启动服务,由于已经指定了认证规则,在控制台也不会打印随机生成的口令了。这时我们通过postman再去请求,发现服务端抛出了如下异常:
java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
at org.springframework.security.crypto.password.DelegatingPasswordEncoder$UnmappedIdPasswordEncoder.matches(DelegatingPasswordEncoder.java:244) ~[spring-security-core-5.1.13.RELEASE.jar:5.1.13.RELEASE]
at org.springframework.security.crypto.password.DelegatingPasswordEncoder.matches(DelegatingPasswordEncoder.java:198) ~[spring-security-core-5.1.13.RELEASE.jar:5.1.13.RELEASE]
at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$LazyPasswordEncoder.matches(WebSecurityConfigurerAdapter.java:605) ~[spring-security-config-5.1.13.RELEASE.jar:5.1.13.RELEASE]
at org.springframework.security.authentication.dao.DaoAuthenticationProvider.additionalAuthenticationChecks(DaoAuthenticationProvider.java:90) ~[spring-security-core-5.1.13.RELEASE.jar:5.1.13.RELEASE]
at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:166) ~[spring-security-core-5.1.13.RELEASE.jar:5.1.13.RELEASE]
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:175) ~[spring-security-core-5.1.13.RELEASE.jar:5.1.13.RELEASE]
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:200) ~[spring-security-core-5.1.13.RELEASE.jar:5.1.13.RELEASE]
at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilterInternal(BasicAuthenticationFilter.java:180) ~[spring-security-web-5.1.13.RELEASE.jar:5.1.13.RELEASE]
经过查阅资料,了解到Spring security 5.0中新增了多种加密方式,使得当进行验证时Spring Security将传输的数据看作是进行了加密后的数据,在匹配之后发现找不到正确识别序列,就认为id是null,因此要将前端传过来的密码进行某种方式加密。
@Override
protected void configure(AuthenticationManagerBuilder authManagerBuilder) throws Exception {
authManagerBuilder.inMemoryAuthentication()
.passwordEncoder(new BCryptPasswordEncoder())
.withUser("eddy")
.password(new BCryptPasswordEncoder().encode("123456"))
.roles("user");
}
再次测试,成功返回!

Spring Security 学习+实践的更多相关文章
- [转]Spring Security学习总结一
[总结-含源码]Spring Security学习总结一(补命名空间配置) Posted on 2008-08-20 10:25 tangtb 阅读(43111) 评论(27) 编辑 收藏 所属分 ...
- Spring security 学习 (自助者,天助之!)
自己努力,何必要强颜欢笑的求助别人呢? 手心向下不求人! Spring security学习有进展哦: 哈哈! 1.页面都是动态生产的吧! 2.设置权限: a:pom.xml配置jar包 b:cr ...
- SpringBoot + Spring Security 学习笔记(五)实现短信验证码+登录功能
在 Spring Security 中基于表单的认证模式,默认就是密码帐号登录认证,那么对于短信验证码+登录的方式,Spring Security 没有现成的接口可以使用,所以需要自己的封装一个类似的 ...
- SpringBoot + Spring Security 学习笔记(三)实现图片验证码认证
整体实现逻辑 前端在登录页面时,自动从后台获取最新的验证码图片 服务器接收获取生成验证码请求,生成验证码和对应的图片,图片响应回前端,验证码保存一份到服务器的 session 中 前端用户登录时携带当 ...
- [转]Spring Security学习总结二
原文链接: http://www.blogjava.net/redhatlinux/archive/2008/08/20/223148.html http://www.blogjava.net/red ...
- spring security 学习资料
spring security 学习资料 网址 Spring Security 文档参考手册中文版 https://springcloud.cc/spring-security.html
- Spring Security学习笔记
Spring Web Security是Java web开发领域的一个认证(Authentication)/授权(Authorisation)框架,基于Servlet技术,更确切的说是基于Servle ...
- Spring Security简明实践及相关国际化处理
别人的都是最佳实践,因为我目前的设置没有按照参考文档推荐,还是采用DelegatingFilterProxy,所以我只能说简明实践.先贴我的applicationContext-security.xm ...
- SpringBoot + Spring Security 学习笔记(二)安全认证流程源码详解
用户认证流程 UsernamePasswordAuthenticationFilter 我们直接来看UsernamePasswordAuthenticationFilter类, public clas ...
随机推荐
- Windows下NodeJS安装与npm环境变量配置
node.js下载:https://nodejs.org/en/download/ 参考:https://www.jianshu.com/p/812de13f1276 1.安装过程基本直接" ...
- C# 二维码生成 ( QRCoder )
二维码1.前言seaconch 最近在搞二维码方面的一些东西,所以接触了一些二维码相关,那么既然用过了就要有用过了的样子 其实关于二维码的文章真的多的数不胜数,有很多写的很认真,很好,但这就像是学习一 ...
- 关于Ubuntu18.04 linux系统使用搜狗输入法 出现乱码
解决: 执行下面的命令即可!无需重启系统 killall fcitx
- WPF 勾选划线
最近项目需要一个左右侧一对多的划线功能 我们先来看一下效果秃: 主要功能: 支持动态添加 支持复选 支持修改颜色 支持动态宽度 主要实现:事件的传递 应用场景:购物互选,食品搭配,角色互选 数据源 左 ...
- Git脑图
ps:有时我们想一台有不同的git账号对应不同的git仓库时(gitLab/gitHub)时,除了全局的用户配置定义,我们可以为不同仓库自定义不同用户名和邮件 1.查询全局的配置:git config ...
- SpringBoot中的application.properties外部注入覆盖
由想要忽略properties中的某些属性,引发的对SpringBoot中的application.properties外部注入覆盖,以及properties文件使用的思考. SpringBoot 配 ...
- SprngCloud微服务框架搭建(一)
参照来源 :https://blog.csdn.net/forezp/article/details/70148833 1.简介 目前来说,SpringCloud是比较完整的微服务解决方案框架.不像其 ...
- playwright-python 处理Text input、Checkboxs 和 radio buttons(三)
Text input 输入框输入元素,直接用fill方法即可,支持 <input>,<textarea>, [contenteditable] 和<label>这些 ...
- tree命令出现乱码
alias tree='tree --charset ASCII'就可以了
- js与jquery获取input输入框中的值
如何用javascript获取input输入框中的值,js/jq通过name.id.class获取input输入框中的value 先准备一段 HTML 一.jquery获取input文本框中的值 通过 ...