从 Postman 请求中读取 Basic Authorization 的用户名和密码值 Spring Boot
Read username and password value of Basic Authorization from Postman request Spring Boot
我对 Spring-boot
很陌生,
我正在构建微服务,它将简单地将请求转发给其他系统进行处理(JSON 到 XML)。
为此,连同请求我需要设置用户名和密码,所以到目前为止我用谷歌搜索我只找到下面的片段。
@Configuration
@EnableWebSecurity
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Autowired
private MyBasicAuthenticationEntryPoint authenticationEntryPoint;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user1").password(passwordEncoder().encode("user1Pass"))
.authorities("ROLE_USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/securityNone").permitAll()
.anyRequest().authenticated()
.and()
.httpBasic()
.authenticationEntryPoint(authenticationEntryPoint);
http.addFilterAfter(new CustomFilter(),
BasicAuthenticationFilter.class);
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
这里是硬编码的,我们不需要这个,也不想维护database
。我正在尝试获取控制器中 Postman
(基本授权)的 Auth
选项卡的用户名和密码。
这样,我们就可以简单地通过请求转发它。
您提供的代码片段用于 use-case,您的服务对用户进行身份验证。
但据我了解,您只是想将凭据转发给另一个服务并让下游服务处理身份验证。
因此,凭据通过“授权”HTTP header [1] 发送。
如果你想访问它们,你可以简单地从请求 (HttpServletRequest.java [2,3]) 中获取它,如下所示:
public ResponseEntity<DemoClass> getDemo(HttpServletRequest request) {
final String authorizationHeader = request.getHeader(HttpHeaders.AUTHORIZATION);
// ...
您得到的值是 Base64[4] 编码的,因此您必须对其进行解码。
例如,使用用户名“username”和密码“password”的基本授权如下所示:
Basic dXNlcm5hbWU6cGFzc3dvcmQ=
首先,必须删除前缀“Basic”,然后您只有用户名和密码的 Base64 编码。
解码后为:
username:password
一种更简单的方法是仅从用户请求中获取授权 header 并将其放入您的请求中。
例如,如下所示:
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
Request request = new Request.Builder()
.url("http://localhost:8080/")
.method("GET", null)
.addHeader("Authorization", "Basic dXNlcm5hbWU6cGFzc3dvcmQ=") // Client credentials from the header
.build();
注意:我只是从邮递员示例中提取的 (Java - OkHttp)。
更多信息:
我是通过以下简单的方法实现的。
import org.apache.commons.codec.binary.Base64;
@PostMapping("/servce-uri")
public ResponseEntity<Result> announce(@RequestHeader("Authorization") String authentication,@Valid @RequestBody RequestDTO requestDto) {
String pair=new String(Base64.decodeBase64(authentication.substring(6)));
String userName=pair.split(":")[0];
String password=pair.split(":")[1];
// call to service
return ResponseEntity.status(HttpStatus.OK).body(result);
}
我对 Spring-boot
很陌生,
我正在构建微服务,它将简单地将请求转发给其他系统进行处理(JSON 到 XML)。
为此,连同请求我需要设置用户名和密码,所以到目前为止我用谷歌搜索我只找到下面的片段。
@Configuration
@EnableWebSecurity
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Autowired
private MyBasicAuthenticationEntryPoint authenticationEntryPoint;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user1").password(passwordEncoder().encode("user1Pass"))
.authorities("ROLE_USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/securityNone").permitAll()
.anyRequest().authenticated()
.and()
.httpBasic()
.authenticationEntryPoint(authenticationEntryPoint);
http.addFilterAfter(new CustomFilter(),
BasicAuthenticationFilter.class);
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
这里是硬编码的,我们不需要这个,也不想维护database
。我正在尝试获取控制器中 Postman
(基本授权)的 Auth
选项卡的用户名和密码。
这样,我们就可以简单地通过请求转发它。
您提供的代码片段用于 use-case,您的服务对用户进行身份验证。
但据我了解,您只是想将凭据转发给另一个服务并让下游服务处理身份验证。
因此,凭据通过“授权”HTTP header [1] 发送。 如果你想访问它们,你可以简单地从请求 (HttpServletRequest.java [2,3]) 中获取它,如下所示:
public ResponseEntity<DemoClass> getDemo(HttpServletRequest request) {
final String authorizationHeader = request.getHeader(HttpHeaders.AUTHORIZATION);
// ...
您得到的值是 Base64[4] 编码的,因此您必须对其进行解码。
例如,使用用户名“username”和密码“password”的基本授权如下所示:
Basic dXNlcm5hbWU6cGFzc3dvcmQ=
首先,必须删除前缀“Basic”,然后您只有用户名和密码的 Base64 编码。 解码后为:
username:password
一种更简单的方法是仅从用户请求中获取授权 header 并将其放入您的请求中。
例如,如下所示:
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
Request request = new Request.Builder()
.url("http://localhost:8080/")
.method("GET", null)
.addHeader("Authorization", "Basic dXNlcm5hbWU6cGFzc3dvcmQ=") // Client credentials from the header
.build();
注意:我只是从邮递员示例中提取的 (Java - OkHttp)。
更多信息:
我是通过以下简单的方法实现的。
import org.apache.commons.codec.binary.Base64;
@PostMapping("/servce-uri")
public ResponseEntity<Result> announce(@RequestHeader("Authorization") String authentication,@Valid @RequestBody RequestDTO requestDto) {
String pair=new String(Base64.decodeBase64(authentication.substring(6)));
String userName=pair.split(":")[0];
String password=pair.split(":")[1];
// call to service
return ResponseEntity.status(HttpStatus.OK).body(result);
}