2 回答

TA贡献1828条经验 获得超4个赞
尝试添加sec:authorize="isAuthenticated()
到要显示用户名的“/account/login”模板
例如:
<h3 sec:authorize="isAuthenticated()" th:text="${user.username}"></h3>
如果它发生了,它将获得身份验证状态,否则,它不会显示<h3>
代码块。

TA贡献1982条经验 获得超2个赞
@Component
public class MyBasicAuthenticationEntryPoint extends BasicAuthenticationEntryPoint {
@Override
public void afterPropertiesSet() throws Exception {
setRealmName("Baeldung");
super.afterPropertiesSet();
}
@Override
public void commence(
HttpServletRequest request, HttpServletResponse response, AuthenticationException authEx)
throws IOException, ServletException {
response.addHeader("WWW-Authenticate", "Basic realm="" + getRealmName() + """);
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
PrintWriter writer = response.getWriter();
writer.println("HTTP Status 401 - " + authEx.getMessage());
}
}
// inside filter we can get the
SecurityContextHolder.getContext().getAuthentication().getPrincipal()
添加回答
举报