springboot-springSecurity 之 http Basic认证 (四)
2017-03-07 15:22
363 查看
引言:
HTTP基础认证(BA)是一种简单的认证机制。当一个web客户端需要保护任何web资源的时候,服务器会发送一个带有401状态码(未授权)的HTTP回应,还有类似WWW-Authenticate: Basic realm=”realm here” 的 WWW-Authenticate HTTP头。而浏览器这时候就会弹出一个登录对话框,提示输入用户名和密码。
1. 修改配置
在spring boot项目中实现Spring Security进行http Basic认证非常简单,只需在配置文件中增加 .httpBasic();直接配置即可使用@Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UrlUserService urlUserService; @Override protected void configure(HttpSecurity http) throws Exception { http .csrf().disable() .authorizeRequests() .antMatchers(DATA_COLLECT_RAW_URL).permitAll() .antMatchers("/login").permitAll() .antMatchers("/logout").permitAll() .antMatchers("/images/**").permitAll() .antMatchers("/js/**").permitAll() .antMatchers("/css/**").permitAll() .antMatchers("/fonts/**").permitAll() .antMatchers("/").permitAll() .anyRequest().authenticated() .and() .sessionManagement() .and() .logout() .and() .httpBasic(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(urlUserService).passwordEncoder(new PasswordEncoder() {//此处为密码使用md5 进行加密 @Override public String encode(CharSequence rawPassword) { return MD5Util.encode((String) rawPassword); } @Override public boolean matches(CharSequence rawPassword, String encodedPassword) { return encodedPassword.equals(MD5Util.encode((String) rawPassword)); } }); } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
2. 登录方式的变化
http Basic 实际上就是将我门的用户名和密码连接起来然后 使用base64进行加密,将加密后的密文放在http 的header 中进行验证。帐号密码加密如下 (假设账号密码都为admin) admin:admin base64 加密后为 YWRtaW46YWRtaW4= 加密后的串放入 header 时应在拼结为 Basic YWRtaW46YWRtaW4= 注意:Basic 与密码串之间为一个空格
postMan 请求如下:
3.在controller 中获取请求参数
由于登录是security 进行验证的,验证成功后会跳转到 “/login“ api,所以我门要定义自己login api@AuthenticationPrincipal 注解是为了从security 中获取登录后的user 信息。
登录成功后返回用户信息。
当登出后也会进入”/login” api ,登出成功返回null
@RequestMapping(value = "/login") @ResponseBody //用户名密码是用base64 加密 原文为 admin:admin 即 用户名:密码 内容是放在request.getHeader 的 "authorization" 中 public Object login(@AuthenticationPrincipal User loginedUser, @RequestParam(name = "logout", required = false) String logout) { if (logout != null) { return null; } if (loginedUser != null) { return loginedUser; } return null; }1
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
5
6
7
8
9
10
11
12
关于security 的Http Basic 验证到此就告一段落了。敬请期待。
相关文章推荐
- springboot-springSecurity 之 http Basic认证 (四)
- Spring Boot实战之Filter实现简单的Http Basic认证
- 第二十七章 springboot + zipkin(brave-okhttp实现)
- Spring Boot Http 406 error
- Spring Boot 运行原理 - 实例分析(HttpEncodingAutoConfiguration)
- Spring Boot工程支持HTTP和HTTPS,HTTP重定向HTTPS
- spring boot 使用@RestController 和 @Controller 构造常见http get ,post请求例子
- SpringSecurity 3.x,无法找到元素 'http' ,以及 [http://www.springframework.org/schema/security]
- springboot自定义http反馈状态码
- Spring Boot HTTP over JSON 的错误码异常处理
- springboot http2
- spring boot HttpMediaTypeNotSupportedException
- 第二十七章 springboot + zipkin(brave-okhttp实现)
- spring boot给http添加正向代理
- spring boot actuator工作原理之http服务暴露源码分析
- 第二十八章 springboot + zipkin(brave定制-AsyncHttpClient)
- SpringBoot对SpringSecurity的支持
- Springboot 实现 Restful 服务,基于 HTTP / JSON 传输
- SpringBoot自定义HttpMessageConverter