【发布时间】:2019-01-25 21:17:18
【问题描述】:
假设User 可以喜欢Posts。当帖子被点赞时,帖子作者(另一个User)的得分会增加。我在数据库中更新作者的状态:
post.getAuthor().incrementScore(LIKE_SCORE);
postRepository.save(post); \\ author is also updated
但问题是,如果作者当前已登录,则更改不会反映给他,他应该重新登录才能看到他的更新分数。
如何在 Spring Security 中更新另一个经过身份验证的用户的状态?
请注意,我没有将用户添加到我的控制器,而是直接在模板中访问主体。我做错了吗?
用户实体:
@Entity
@EqualsAndHashCode(of = "username")
public class User implements UserDetails {
@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;
@ManyToMany(mappedBy = "likers", fetch = EAGER)
private Set<Post> favorites = new HashSet<>();
@OneToMany(mappedBy = "author", fetch = EAGER)
private Set<Post> posts = new HashSet<>();
@NaturalId
private String username;
private String password;
private long score;
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return Set.of(new SimpleGrantedAuthority("ROLE_" + role));
}
@Override
public String getPassword() {
return password;
}
@Override
public String getUsername() {
return username;
}
// other overridden methods, getters and setters
}
安全配置:
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(encoder);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll()
.and().formLogin().usernameParameter("username").passwordParameter("password")
.loginProcessingUrl("/login")
.successHandler((request, response, authentication) ->
response.getOutputStream().print(true))
.failureHandler((request, response, exception) ->
response.getOutputStream().print(""))
.and().logout()
.logoutSuccessHandler((request, response, authentication) ->
response.sendRedirect(request.getHeader("referer")))
.and().cors().disable();
}
Thymeleaf 模板:
<div class="nav">
<div class="author">
<div class="usr-name" th:text="${#authentication.principal.username}"></div>
<div class="score" th:text="${#authentication.principal.score}"></div>
</div>
</div>
【问题讨论】:
-
如何加载用户?您是否将用户保存在 HTTP 会话中。显示你的 Spring Security 配置和你的控制器,它会显示分数。
标签: java hibernate spring-boot spring-security spring-data