Spring Security 之API 项目安全验证(基于basic-authentication)
===================================
Basic Authorization 规范
===================================
Request 头部:
Authorization: Basic QWxpY2U6MTIzNDU2
其中 QWxpY2U6MTIzNDU2 是user:pwd做 base64 编码, 格式是 user:pwd
response 头部:
WWW-Authenticate: Basic realm="My Realm"
按照 RFC 规范, 相同的 realm(域) 下的web page 将共享同样的 credentials, 所以推荐 realm 取值为 application name. realm 大小写敏感, 可以包含空格.
===================================
Rest API 示例
===================================
功能:
1. 演示如何启用 Basic Authorization
2. 如何使用 RestTemplate 访问受保护的 API接口
-----------------------------------
SecurityConfig 代码
-----------------------------------
关键点有:
0. 对于 /api/** 需要 ROLE_ADMIN 角色的账号访问, 对于 /guest/** 路径允许匿名访问.
1. 使用 HttpSecurity.httpBasic() 启用 Basic Authorization.
2. 使用 HttpSecurity.httpBasic().realmName() 设置 realm.
3. 使用 HttpSecurity.httpBasic().authenticationEntryPoint() 设置 BasicAuthenticationEntryPoint 对象, 如果一个请求通过验证, 该对象会自动为web response设定 WWW-Authenticate header, 如果未通过, 该对象会自动将HttpStatus设置为UNAUTHORIZED.
4. 显式启用了 STATELESS session 管理机制, 经测试,Spring Security 在Basic Authorization模式下, session自动就处于了 STATELESS 状态.
5. 对于 HttpMethod.OPTIONS 请求, 允许匿名访问. API 项目应该开放 OPTIONS 查询权限.
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter { //@formatter:off
@Override
public void configure(AuthenticationManagerBuilder builder) throws Exception {
builder.inMemoryAuthentication()
.withUser("123").password("123").roles("USER")
.and()
.withUser("ADMIN").password("ADMIN").roles("ADMIN");
}
//@formatter:on private static String REALM = "MY SPRING SECURITY DEMO"; // @formatter:off
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
// 对于/api 路径下的访问需要有 ROLE_ADMIN 的权限
.antMatchers("/api/**").hasRole("ADMIN")
// 对于/guest 路径开放访问
.antMatchers("/guest/**").permitAll()
// 其他url路径之需要登陆即可.
.anyRequest().authenticated()
.and()
//启用 basic authentication
.httpBasic().realmName(REALM).authenticationEntryPoint(getBasicAuthenticationEntryPoint())
.and()
//不创建 session
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
// @formatter:on /**
* 生成 BasicAuthenticationEntryPoint 对象, 在该对象的支持下, 通过验证的请求, 返回的response 将会自动加上
* WWW-Authenticate Header. 在该对象的支持下, 未通过验证的请求, 返回的 response 为 UNAUTHORIZED 错误.
*
* @return
*/
@Bean
public BasicAuthenticationEntryPoint getBasicAuthenticationEntryPoint() {
BasicAuthenticationEntryPoint entryPoint = new BasicAuthenticationEntryPoint();
entryPoint.setRealmName(REALM);
return entryPoint;
} /*
* 开放 Options 请求
*/
@Override
public void configure(WebSecurity web) throws Exception {
// TODO Auto-generated method stub
web.ignoring()
.antMatchers(HttpMethod.OPTIONS, "/**");
} @SuppressWarnings("deprecation")
@Bean
public NoOpPasswordEncoder passwordEncoder() {
BasicAuthenticationEntryPoint a;
return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
}
}
-----------------------------------
受控 API Rest 类
-----------------------------------
对于 /api/** url需要 ROLE_ADMIN 角色的账号访问, 这里的代码很简单.
/*
* 需要进行 Basic Auth 的API
*/
@RestController
@RequestMapping("/api")
class ApiController {
@GetMapping("/books")
public String getBooks() {
return "API book";
}
}
直接访问受控url, 弹出浏览器内置的登陆框, 见下图, 符合预期.

-----------------------------------
RestTemplate 访问 Basic Authorization的API
-----------------------------------
关键点:
使用了 restTemplateBuilder.basicAuthorization(user,pwd).build() 来构建 RestTemplate, 这样的 RestTemplate 会自动在Request上加 Authorization Header.
/*
* RestTemplate 访问 Basic Authorization的API的示例
*/
@RestController
@RequestMapping("/guest")
class DefaultController { @GetMapping("/mybooks")
@ResponseBody
public String getMyBook() {
return "My Book";
} private RestTemplate restTemplate; @Autowired
private RestTemplateBuilder restTemplateBuilder; @Autowired
void setRestTemplate(RestTemplateBuilder restTemplateBuilder) {
restTemplate = restTemplateBuilder.basicAuthorization("ADMIN", "ADMIN")
.build();
} @GetMapping("/apibooks")
@ResponseBody
public String getApiBooks() {
return restTemplate.getForObject("http://localhost:8080/api/books", String.class); }
}
访问 http://localhost:8080/guest/apibooks 地址, 无需登陆即可得到 api 结果, 见下图, 符合预期.

===================================
参考
===================================
http://websystique.com/spring-security/secure-spring-rest-api-using-basic-authentication/
http://www.bytestree.com/spring/restful-web-services-authentication-authorization/
https://www.baeldung.com/spring-security-basic-authentication
Spring Security 之API 项目安全验证(基于basic-authentication)的更多相关文章
- spring security +MySQL + BCryptPasswordEncoder 单向加密验证 + 权限拦截 --- 心得
1.前言 前面学习了 security的登录与登出 , 但是用户信息 是 application 配置 或内存直接注入进去的 ,不具有实用性,实际上的使用还需要权限管理,有些 访问接口需要某些权限才可 ...
- 使用Spring MVC测试Spring Security Oauth2 API
不是因为看到希望了才去坚持,而坚持了才知道没有希望. 前言 在Spring Security源码分析十一:Spring Security OAuth2整合JWT和Spring Boot 2.0 整合 ...
- Spring Security(十五):5.6 Authentication
Thus far we have only taken a look at the most basic authentication configuration. Let’s take a look ...
- Spring Security 入门(3-11)Spring Security 的使用-自定义登录验证和回调地址
配置文件 security-ns.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmln ...
- Spring Security构建Rest服务-0700-SpringSecurity开发基于表单的认证
自定义用户认证逻辑: 1,处理用户信息获取,2,用户校验,3密码的加密解密 新建:MyUserDetailService类,实现UserDetailsService接口. UserDetailsSer ...
- Secure Spring REST API using Basic Authentication
What is Basic Authentication? Traditional authentication approaches like login pages or session iden ...
- 【项目实践】一文带你搞定Spring Security + JWT
以项目驱动学习,以实践检验真知 前言 关于认证和授权,R之前已经写了两篇文章: [项目实践]在用安全框架前,我想先让你手撸一个登陆认证 [项目实践]一文带你搞定页面权限.按钮权限以及数据权限 在这两篇 ...
- JAVA WEB快速入门之从编写一个基于SpringBoot+Mybatis快速创建的REST API项目了解SpringBoot、SpringMVC REST API、Mybatis等相关知识
JAVA WEB快速入门系列之前的相关文章如下:(文章全部本人[梦在旅途原创],文中内容可能部份图片.代码参照网上资源) 第一篇:JAVA WEB快速入门之环境搭建 第二篇:JAVA WEB快速入门之 ...
- spring security 一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架
Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文中 配置的Bean,充分利用了Spring ...
随机推荐
- Loj 2320.「清华集训 2017」生成树计数
Loj 2320.「清华集训 2017」生成树计数 题目描述 在一个 \(s\) 个点的图中,存在 \(s-n\) 条边,使图中形成了 \(n\) 个连通块,第 \(i\) 个连通块中有 \(a_i\ ...
- springboot + mybatis +pageHelper分页排序
今天下午写查出来的数据的排序,原来的数据没有排序,现在把排序功能加上...原来用的,是xml中的sql动态传参 ,,1个小数没有弄出来,果断放弃... 网上百度一下,发现用pageHelper 可以 ...
- 《JAVA程序设计》_第四周学习总结
一.本周学习内容 1.子类与父类--5.1知识 在类的声明中用关键字extends来定义一个类的子类,格式如下: class 子类名 extends 父类名 { ... } 2.子类的继承性--5.2 ...
- 利用SQL注入漏洞登录后台
所谓SQL注入,就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令,比如先前的很多影视网站泄露VIP会员密码大多就是通过WEB表单递交查询 ...
- 错误代码1045 Access denied for user 'root'@'localhost' (using password:YES)
在mysql中新建连接,ip地址是127.0.0.1,账号是root,密码是123456,但是测试连接的时候报错, 错误代码1045 Access denied for user 'root'@'lo ...
- 分布式任务调度平台XXL-JOB搭建教程
关于分布式任务调度平台XXL-JOB,其实作者 许雪里 在其发布的中文教程中已经介绍的很清楚了,这里我就不做过多的介绍了,关于其搭建教程,本人依照其文档搭建起来基本上也没遇到啥问题,这里通过博客的形式 ...
- 安装软件the error code is 2203解决方法
win10安装mysql5.7的时候弹出这个2203错误,记录一下. 解决方法: 按照下面路径,去掉只读解决了.
- 【Swift 3.0】iOS 国际化切换语言
有的 App 可能有切换语言的选项,结合系统自动切换最简单的办法: fileprivate var localizedBundle: Bundle = { return Bundle(path: Bu ...
- 一个.net的程序员如何转到java的?
先说明,大佬请忽略我这篇文章, 我是一个做了5年的纯种C#开发人, 我在此仅记录一下我转java的过程.都知道, java是开源的,所以它的开发工具贼多,不像.net, 直接地表最强的IDE. 像现 ...
- CSL 的魔法
链接 [https://ac.nowcoder.com/acm/contest/551/E] 分析 很显然就是a的第k大得和b的倒数第k大相乘. 那么我们只要让a的第k大和b的倒数第k大位置是相同的即 ...