java - Spring 自定义身份验证令牌从未 GCed
问题描述
我已经实现了自定义 AuthenticationProvider:
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String name = authentication.getName();
String password = authentication.getCredentials().toString();
List<GrantedAuthority> grantedAuths = new ArrayList<>();
grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
return new PassthroughAuthentication(name, password, grantedAuths);
}
直通身份验证:
public class PassthroughAuthentication implements Authentication {
private static final long serialVersionUID = 1L;
private String username;
private boolean authenticated;
private Object password;
private List<GrantedAuthority> grantedAuthorities;
public PassthroughAuthentication(String username, String password, List<GrantedAuthority> grantedAuthorities){
super();
this.username = username;
this.password = password;
this.grantedAuthorities = grantedAuthorities;
}
@Override
public String getName() {
return this.username;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return this.grantedAuthorities;
}
@Override
public Object getCredentials() {
return this.password;
}
@Override
public Object getDetails() {
return null;
}
@Override
public Object getPrincipal() {
return new User(username, password.toString(), grantedAuthorities );
}
@Override
public boolean isAuthenticated() {
return authenticated;
}
@Override
public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
this.authenticated = isAuthenticated;
}
}
而且我可以使用 VisualVM 看到PassthroughAuthentication
每个请求的对象都保留在内存中,并且永远不会被 GC 处理。
但是我的请求是无状态的,并且我禁用了会话管理。
什么可以举办这个班?
解决方案
想通了。
代替:
http.sessionManagement().disable();
无状态会话创建解决了这个问题:
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
推荐阅读
- r - 在数据框中的组内按时间向后编号代数(gen:0、-1、-2 等)
- javascript - Azure Maps 上是否有任何绘图功能?
- c# - 如何在 LINQ 中将行转换为列
- elasticsearch - ElasticSearch 有什么好的布尔查询解析器吗?
- c++ - 通过索引运算符插入 boost::program_options::variables_map
- mockito - 由于控制器方法内部的方法调用,MockMvc 失败
- angular - 在我的函数 ngDoCheck() Angular 中关闭 matDialog 时出现问题
- python - 使用 Python 从字符串中提取值
- typescript - 在观察者运行之前将 DOM.element 分配给模块变量
- sql - Restore Backups automatically using SQL Server