spring-boot 请求为什么会出现jsessionid=856DC0387CAE5FFCBD4B4A3810AFB646,请给出解决方法。或者代码
时间: 2024-03-27 07:39:29 浏览: 17
在Spring Boot应用中,如果使用了Session机制,请求时会自动带上JSESSIONID,如果不想在URL中看到JSESSIONID,可以通过以下两种方式解决:
1. 使用Cookie存储Session ID:
在Spring Boot中,可以通过配置将Session ID存储在Cookie中,而不是URL中。在application.properties文件中添加以下配置:
```
server.servlet.session.cookie.name=MYSESSIONID
server.servlet.session.cookie.http-only=true
server.servlet.session.cookie.path=/
server.servlet.session.cookie.max-age=1800
```
其中,MYSESSIONID是自定义的Session ID名称,http-only表示Cookie只能通过HTTP或HTTPS传输,不能通过JavaScript获取或修改,path表示Cookie的路径,max-age表示Cookie的过期时间,单位为秒。
2. 使用URL重写隐藏JSESSIONID:
在Spring Boot中,可以通过配置URL重写来隐藏JSESSIONID。在WebSecurityConfigurerAdapter中添加以下配置:
```java
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
// ...
.sessionManagement()
.sessionFixation().migrateSession()
.sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
.sessionAuthenticationStrategy(sessionAuthenticationStrategy())
.sessionConcurrency(sessionConcurrency())
.invalidSessionUrl("/login?timeout")
.maximumSessions(2).maxSessionsPreventsLogin(true)
.expiredUrl("/login?expired")
.and()
.and()
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/")
.invalidateHttpSession(true)
.deleteCookies("JSESSIONID")
.and()
.and()
.portMapper()
.http(8080).mapsTo(8443)
.http(80).mapsTo(443)
.and()
.and()
.rememberMe()
.userDetailsService(userDetailsService())
.tokenValiditySeconds(1209600)
.key("uniqueAndSecret");
}
@Bean
public HttpSessionIdResolver httpSessionIdResolver() {
return HeaderHttpSessionIdResolver.xAuthToken(); // 通过HTTP头传递Session ID
}
}
```
其中,HeaderHttpSessionIdResolver.xAuthToken()表示通过HTTP头传递Session ID,也可以使用其他方式传递Session ID。
以上是两种解决方法,可以根据实际情况选择。
相关推荐
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)