关于java:调试Http-Basic认证用base64加密解密

6次阅读

共计 1129 个字符,预计需要花费 3 分钟才能阅读完成。

我最新最全的文章都在 南瓜慢说 www.pkslow.com,欢送大家来喝茶!

1 HTTP Basic 认证

HTTP Basic认证是在 HTTP 1.0 就引入的认证计划,存在平安缺点;但因为实现简略,仍有我的项目在用。

它次要通过申请头 Authorization 来做认证,格局为:

键:Authorization

值:Basic base64(username:password),即Basic 加密串,如Basic dXNlcjp1c2Vy

Spring Security的配置能够为:

@EnableWebFluxSecurity
public 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 base64
dXNlcjp1c2Vy

# 加密
$ echo -n 'user:user' | base64
dXNlcjp1c2Vy

# 加密
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'

欢送关注微信公众号 <南瓜慢说>,将继续为你更新 …

多读书,多分享;多写作,多整顿。

正文完
 0