精通Spring Boot——第十六篇:初探Spring Security,使用Http Basic认证
原
荐
字数 734
阅读 105
收藏 8
Spring Security Spring Spring Boot
说明
本文以及接下来有关spring security 的文章, 基于Spring Boot 2.1.0 RELEASE , Spring Security 5.1.2RELEASE
简单介绍Spring Security
Spring Security是当今非常流行的,基于Spring提供了一套Web安全性的完整框架。用于对用户进行认证(Authentication)和授权(Authorization)。在用户认证方面,Spring Security 支持主流的验证方式,包括,HttpBasic认证,Http表单认证,Http摘要认证,OpenId以及LDAP(轻量目录访问协议:Lightweight Directory Access Protocol)等。在用户授权方面,Spring Security 提供了基于角色的访问控制和访问控制列表(Access Control List,ACL),可以对应用中的领域对象进行细粒度的控制。 本文将通过介绍如何在Spring Boot项目中使用Spring Security保护应用,我们先讨论如何自定义用户的认证逻辑,通过Spring Security 提供的UserDetailService,User对象,密码加密PasswordEncoder来初步认识Spring Security。
初探:用httpBasic认证
Spring Security 5.X 和Spring Security 4.X 在Http Basic认证有些不同,在Spring Security4.X中,我们想要使用Http Basic认证只需要如下代码:
/**
* @author developlee
* @since 2018/11/17 22:43
*/
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/index").permitAll()
.anyRequest().authenticated()
.and().httpBasic();
super.configure(http);
}
}
Spring Security 是默认开启了 Http Basic认证的,如果想要关闭可以设置 security.basic.enabled: false (Spring Security5.X中已弃用) 而Spring Security 5.X的实现则有些不同,如果按照以上代码,则访问链接时,会跳转至Spring Security 提供的默认登陆页。接下来看看Spring Security5.X的实现,文档是这样描述的: 也就是说,要将BasicAuthenticationFilter添加到Spring Security的filterChain中。let's do it! 我们先继承BasicAuthenticationEntryPoint,重写commence方法。
/**
* @author developlee
* @since 2018/11/25 11:36
*/
@Component
public class MyBasicAuthenticationEntryPoint extends BasicAuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
response.addHeader("WWW-Authenticate", "Basic realm=" + getRealmName());
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
PrintWriter printWriter = new PrintWriter(response.getOutputStream());
printWriter.write("Http Status 401: " + authException.getLocalizedMessage());
}
@Override
public void afterPropertiesSet() throws Exception {
setRealmName("developlee");
super.afterPropertiesSet();
}
}
接下来看看如何配置
/**
* @author developlee
* @since 2018/11/17 22:43
*/
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private MyBasicAuthenticationEntryPoint authenticationEntryPoint;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/login").permitAll()
.anyRequest().authenticated()
.and().httpBasic()
.authenticationEntryPoint(authenticationEntryPoint);
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("lensen").password(passwordEncoder().encode("123456")).authorities("ROLE_USER");
}
@Bean
protected PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
LoginController.java的代码
/**
* @author developlee
* @since 2018/11/17 22:02
*/
@RestController
public class LoginController {
@GetMapping("/hello")
public String hello() {
return "hello";
}
}
启动项目,访问我们写好的链接地址。 http://loalhost:8080/hello
至此,Spring Security 5.X使用Http Basic 登陆的实例便已经完成了。 本文的所有代码我已经放在我的 github.com 上,感谢您的观看,如果有什么错误的地方,还请指出,共同探讨!
© 著作权归作者所有
共有人打赏支持
上一篇: 精通Spring Boot ——第十七篇:Spring Security自定义登录逻辑
下一篇: 精通Spring Boot —— 第十五篇:自定义异常处理
加载中
评论( 2 )
删除一条评论
评论删除后,数据将无法恢复
取消
确定
相关文章 最新文章
Spring Boot 2.x 已经发布了很久,现在 Spring Cloud 也发布了 基于 Spring Boot 2.x 的 Finchley 版本,现在一起为项目做一次整体框架升级。 升级前 => 升级后 Spring Boot 1.5.x => Sprin...
Java技术栈
08/09
0
0
导读 在上一篇文章中对Spring Boot 集成Shrio做了一个简单的介绍,这篇文章中主要围绕Spring Boot 集成 Spring Security展开,文章末尾附有学习资料。 快速上手: 1.引入pom依赖 2.实现一个简...
yangrd
08/27
0
0
RabbitMQ RabbitMQ 安装 linux 安装RabbitMQ详细教程 Ubuntu 16.04 RabbitMq 安装与运行(安装篇) ubantu安装rabbitMQ步骤 多线程 Spring @transactional注解和synchronized同步锁同时使用不...
OSC_fly
07/26
0
0
Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密...
小致dad
08/03
0
0
SpringBoot初探——HelloWorld 一、什么是Spring Boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来...
Qiu_CJ
07/18
0
0
没有更多内容
加载失败,请刷新页面
加载更多摘要: 近日,阿里云重磅发布PostgreSQL 10 高可用本地SSD盘版,相比原 9.4 版本又新增了JSONB、BRIN索引、GROUPING SETS/CUBE/ROLLUP、UPSERT等多项功能。 2015年,阿里云宣布正式推出RDS f...
阿里云云栖社区
27分钟前
4
0
一、目前对账的算法: 1、从上游渠道(银行、银联等金融机构)获取对账文件,程序逐行解析入库 2、在存储过程中,以上游对账文件的表为基准,程序逐行读取并与我们系统的交易记录/账务记录(...
辉煌霸猪
29分钟前
2
0
到此, 所有关于class文件格式的重要内容都已经讲解完了, 不敢说面面俱到, 但是敢说大部分重要的内容都包含在内了。前前后后用了9篇博客来专门讲解class文件结构, 为什么花那么多的时间和...
xtof
30分钟前
1
0
反射:运行时的类信息 运行时类型信息使得你可以在程序运行时发现和使用类型信息 1. Class对象 通过Class对象可以在运行时发现一个对象完整的类继承结构 类是程序的一部分,每一个类都会有一...
不学无数的程序员
47分钟前
2
0
cd /opt/gopath/src/github.com/hyperledger/ git clone https://github.com/hyperledger/fabric.git 下载fabric源码 cd /opt/gopath/src/github.com/hyperledger/ $ git clone https://git......
八戒八戒八戒
52分钟前
2
0
没有更多内容
加载失败,请刷新页面
加载更多以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 圣思园《精通Spring Boot/Cloud》与《精通Java并发》课程现已宣布
- Charles 从入门到精通
- MAT 入门到精通(一)
- Git 从入门到精通
- Webpack入门到精通(1)
- 爬虫入门到精通-网页的下载
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
疯传:让你的产品、思想、行为像病毒一样入侵(全新修订版)
[美] 乔纳•伯杰(Jonah Berger) / 乔迪、王晋 / 电子工业出版社 / 2016-6 / 68.00
是什么让事物变得流行? 从买轿车、买衣服、吃三明治,到给孩子取名字,你是否知道为什么某些产品会大卖,某些故事被人们口口相传,某些电子邮件更易被转发,或者某些视频链接被疯狂地点击,某些谣言更具传播力,某些思想和行为像病毒一样入侵你的大脑……这本书将为你揭示这些口口相传和社会传播背后的科学秘密,并且告诉你如何将产品、思想、行为设计成具有感染力和传播力的内容。 无论你是大公司的管理者,还是努......一起来看看 《疯传:让你的产品、思想、行为像病毒一样入侵(全新修订版)》 这本书的介绍吧!