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 ...
 
随机推荐
- nodejs 更改项目端口号的 方法
			
我这里是 koa2 项目 1.项目目录 serverConf.js 这里面配置端口 代码如下: const ServerConf= { ServicePort: 3036 }; module.expo ...
 - linux下静态库的制作
			
在我们编写软件的过程当中,少不了需要使用别人的库函数.因为大家知道,软件是一个协作的工程.作为个人来讲,你不可能一个人完成所有的工作.另外,网络上一些优秀的开源库已经被业内广泛接受,我们也没有必要把 ...
 - JavaWeb之分页查询
			
时间:2016-12-11 01:41 1.分页的优点: 只查询一页,不需要查询所有数据,能够提高效率.2.分页数据 页面的数据都是由Servlet传递的 * 当前页:pageC ...
 - Android Studio 百度地图导航
			
配置就不再多说了,上一篇已经详细说过了,这次就是根据经纬度坐标做地图导航,路径规划.直接上代码: package com.example.appview.mian_page.Frament.Anzhu ...
 - cs_play
			
# -*-coding:utf-8-*-__author__ = "logan.xu"###构造函数#class Role:# n = 123# # 类变量 比如 n = 123# ...
 - Python之requests模块-response
			
response类故名思议,它包含了服务器对http请求的响应.每次调用requests去请求之后,均会返回一个response对象,通过调用该对象,可以查看具体的响应信息. 示例如下: import ...
 - 一文读懂Lua元表
			
元表 Lua语言中的每种类型的值都有一套可预见的操作集合.例如,我们可以将数字相加,可以连接字符串,还可以在表中插入键值对等,但是我们无法将两个表相加,无法对函数作比较,也无法调用一个字符串,除非使用 ...
 - MySQL——字符集
			
-- 字符集:是一个系统支持的所有抽象字符的集合 MySQL数据库的字符集(包括两个部分): 1.字符集:character 2.校对规则:collation MySQL中常见的字符集: utf8 l ...
 - fwm环境APP菜品数据加载失败的优化操作
			
1)在项目的.env文件中添加如下一行: RESPONSE_CACHE_ENABLED=true 2)拷贝 laravel-worker.conf.example,将laravel字段替换为域名,并执 ...
 - web基础常识
			
1.b/s架构 2.tcp协议 3.web服务器