2 回答

TA貢獻1828條經(jīng)驗 獲得超4個贊
嘗試添加sec:authorize="isAuthenticated()
到要顯示用戶名的“/account/login”模板
例如:
<h3 sec:authorize="isAuthenticated()" th:text="${user.username}"></h3>
如果它發(fā)生了,它將獲得身份驗證狀態(tài),否則,它不會顯示<h3>
代碼塊。

TA貢獻1982條經(jīng)驗 獲得超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()
添加回答
舉報