我最新最全的文章都在 南瓜慢说 www.pkslow.com ,欢送大家来喝茶!
1 HTTP Basic认证
HTTP Basic
认证是在HTTP 1.0
就引入的认证计划,存在平安缺点;但因为实现简略,仍有我的项目在用。
它次要通过申请头Authorization
来做认证,格局为:
键:Authorization
值:Basic base64(username:password)
,即Basic 加密串
,如Basic dXNlcjp1c2Vy
。
Spring Security
的配置能够为:
@EnableWebFluxSecuritypublic class WebfluxSecurityConfig { @Bean public MapReactiveUserDetailsService userDetailsService() { UserDetails user = User.withDefaultPasswordEncoder() .username("user") .password("user") .roles("USER") .build(); return new MapReactiveUserDetailsService(user); } @Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { http.httpBasic() .and() .authorizeExchange() .anyExchange().hasRole("USER") .and() .formLogin() .and() .csrf().disable(); return http.build(); }}
2 如何加密解密
命令如下:
# 加密$ echo -n 'user:user' | openssl base64dXNlcjp1c2Vy# 加密$ echo -n 'user:user' | base64dXNlcjp1c2Vy# 加密openssl base64 -in <infile> -out <outfile># 解密$ echo -n 'dXNlcjp1c2Vy' | base64 -D
当然也能够通过代码加密/解密。
3 拜访
3.1 Postman拜访
通过Postman
能够间接输出用户名和明码拜访,其实它也是帮你主动加个申请头而已。所以要申请,本人不须要再增加申请头Authorization
了,省得笼罩了。
3.2 命令行拜访
通过curl
拜访如下:
$ curl http://localhost:8088/user/name -H 'Authorization:Basic dXNlcjp1c2Vy'
欢送关注微信公众号<南瓜慢说>,将继续为你更新...
多读书,多分享;多写作,多整顿。