gpt4 book ai didi

java - Spring security - 启用 HTTPS - 客户端收到 403 未经授权

转载 作者:行者123 更新时间:2023-12-01 18:24:02 25 4
gpt4 key购买 nike

我正在尝试使用 Spring Security 创建一个演示应用程序,我需要使用 SSL 证书保护我的 Rest API。这些是我创建示例所执行的步骤:1-使用keytool命令生成JKS文件:

keytool -genkey -keyalg RSA -alias tomcat -keystore selfsigned.jks -validity 365 -keysize 2048

2- 将结果 jks 转换为 pkcs12 格式:

keytool -genkey -keyalg RSA -alias tomcat -keystore selfsigned.jks -validity 365 -keysize 2048

3- 创建了一个服务器端应用程序,如下所示:

@EnableWebSecurity
public class X509AuthenticationServer extends WebSecurityConfigurerAdapter {


@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.x509()
.subjectPrincipalRegex("OU=My self signed certificate");
}

}

@Controller
@RequestMapping("/get")
public class UserController {
@PostMapping(value = "/user")
public String user(@RequestBody UserInfo userInfo) {

return "Welcome: " + userInfo.getUsername();
}
}


server.ssl.key-store=/Users/mymacuser/devhome/certpoc/selfsigned.jks
server.ssl.key-store-password=marwan
server.ssl.key-alias=tomcat
server.ssl.key-password=marwan
server.ssl.enabled=true
server.port=8443
spring.security.user.name=Admin
spring.security.user.password=admin
spring.security.require-ssl=true

4- 创建了一个客户端应用程序,如下所示:

@Configuration
public class BeanConfig {

@Bean("httpsClient")
public RestTemplate getSSLRestTemplate() throws IOException, CertificateException, NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException, KeyManagementException {
char[] storePassword = "marwan".toCharArray();
File keyStore = new File("/Users/mymacuser/devhome/certpoc/selfsigned.jks");

SSLContext sslContext = new SSLContextBuilder()
.loadTrustMaterial(keyStore, storePassword)
.loadKeyMaterial(keyStore, storePassword, storePassword)
.build();

SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);

CloseableHttpClient client = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(client);
RestTemplate restTemplate = new RestTemplate(factory);
// restTemplate.setMessageConverters(List.of(new Jaxb2RootElementHttpMessageConverter()));
return restTemplate;
}

@RestController
@RequestMapping("/get")
public class ClientController {

@Autowired
@Qualifier("httpsClient")
private RestTemplate httpsClient;


@RequestMapping("/user")
public String getUserFromServer() throws URISyntaxException {

ResponseEntity<String> s = httpsClient.exchange("https://localhost:8443/get/user", HttpMethod.POST, new HttpEntity<UserInfo>(new UserInfo("marwan")), String.class);
return s.getBody();

}
}

正如您所注意到的,我的客户端是一个(MITM)中间人服务,我从 POSTMAN 或curl 请求调用它。

当我调用 MITM 服务的请求时,我得到响应:

{
"timestamp": "2020-02-17T09:16:20.399+0000",
"status": 500,
"error": "Internal Server Error",
"message": "403 : [{\"timestamp\":\"2020-02-17T09:16:20.325+0000\",\"status\":403,\"error\":\"Forbidden\",\"message\":\"Forbidden\",\"path\":\"/get/user\"}]",
"path": "/get/user"
}

我尝试打印 MITM 和服务器端的网络日志,两者都显示它们正在交换正确的证书

最佳答案

好的,所以我解决了这个问题,如果有人感兴趣的话,这是解决方案:在 TLS 中有两个不同的存储:KeyStore - 用于存储用于向其他对等点出售身份的 key TrustStore - 用户撕毁受信任节点的 key 对于服务器端身份验证,还需要指定信任存储并包含客户端证书。另外,还需要将以下属性添加到 application.properties 文件中:

server.ssl.client-auth=need

因此 application.proprties 应包括:

server.ssl.trust-store=file:/Users/mymacuser/devhome/certpoc/trust.jks
server.ssl.trust-store-type=JKS
server.ssl.trust-store-password=marwan
server.ssl.client-auth=need

关于java - Spring security - 启用 HTTPS - 客户端收到 403 未经授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60259850/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com