精通Spring Boot——第十六篇:初探Spring Security,使用Http Basic认证
说明
本文以及接下来有关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上,感谢您的观看,如果有什么错误的地方,还请指出,共同探讨!
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
vue中的ajax请求和axios包详解
在vue中,经常会用到数据请求,常用的有:vue-resourse、axios 首先,引入axios CDN: <script src="https://unpkg.com/axios/dist/axios.min.js"></script> npm: npm install axios 并在全局的js中引入:import axios from 'axios'; get请求 axios.get('/user?ID=12345') .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); });//欢迎加入全栈开发交流圈一起学习交流:864305860 post请求 //依赖于qs包,将对象转换成以&连接的字符串 //例: axios.post( postUrl ,qs.stringify({userid:1,username:'yyy'})).then(function (response) { consol 配置...
- 下一篇
Mybatis 缓存系统源码解析
本文从以下几个方面介绍: 相关文章 前言 缓存的相关接口 一级缓存的实现过程 二级缓存的实现过程 如何保证缓存的线程安全 缓存的装饰器 相关文章 Mybatis 解析 SQL 源码分析二 Mybatis Mapper.xml 配置文件中 resultMap 节点的源码解析 Mybatis 解析 SQL 源码分析一 Mybatis Mapper 接口源码解析(binding包) Mybatis 数据源和数据库连接池源码解析(DataSource) Mybatis 类型转换源码分析 Mybatis 解析配置文件的源码解析 前言 在使用诸如 Mybatis 这种 ORM 框架的时候,一般都会提供缓存功能,用来缓存从数据库查询到的结果,当下一次查询条件相同的时候,只需从缓存中进行查找返回即可,如果缓存中没有,再去查库;一方面是提高查询速度,另一方面是减少数据库压力;Mybatis 也提供了缓存,它分为一级缓存和二级缓存,接下来就来看看它的缓存系统是如何实现的。 缓存系统的实现使用了 模板方法模式 和 装饰器模式 接下来先来看下和缓存相关的接口 Cache Mybatis 使用 Cache 来表...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- CentOS7,8上快速安装Gitea,搭建Git服务器
- Linux系统CentOS6、CentOS7手动修改IP地址
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- MySQL8.0.19开启GTID主从同步CentOS8
- Red5直播服务器,属于Java语言的直播服务器
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- SpringBoot2整合Redis,开启缓存,提高访问速度
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- CentOS7,CentOS8安装Elasticsearch6.8.6