java - java - 如果登录身份验证在java spring security中失败,如何获取anonymousUser用户名?
问题描述
我正在努力解决过去几周的一个问题。请帮忙 !
org.springframework.security.web.authentication.AnonymousAuthenticationFilter - 使用匿名令牌填充 SecurityContextHolder:'org.springframework.security.authentication.AnonymousAuthenticationToken@905571d8:主体:anonymousUser;凭证:[受保护];已认证:真实;详细信息:org.springframework.security.web.authentication.WebAuthenticationDetails@0: RemoteIpAddress: 0:0:0:0:0:0:0:1; 会话ID:879CE33EC09255373A6D10FAF8B12EE6;授予权限:ROLE_ANONYMOUS'
如果我输入正确的用户名和错误的密码,我想知道为什么用户名是匿名用户?
我想获取我在登录页面中输入的用户名。例如-如果我在用户名中输入管理员并且密码不正确,则用户名应该是管理员而不是匿名用户。
当我这样做时,我auth.getName();
也得到了anonymousUser。
请帮忙 !!!
登录.jsp
<form method="post" action="<c:url value='j_spring_security_check' />">
<div class="row">
<div class="col-md-offset-5 col-md-3">
<div><h2>Login Here</h2> </div>
<div class="form-login">
<h4 style="color: red">${message}</h4>
<input type="text" name="username" id="userName" class="form-control input-sm chat-input" placeholder="Username" required="required" pattern="[0-9]{0,10}" title="Username should not be greater than 10 letters and it should be Alps ID"/>
</br>
<input type="password" name="password" id="userPassword" class="form-control input-sm chat-input" placeholder="Password" required="required" pattern="[A-Za-z0-9!$#%]{8,20}" title="Password should contain A-Z a-z 0-9 ! $ # %"/>
</br>
<!-- <!-- CAPTCHAA -->
<div class="wrapper">
<span class="group-btn">
<input type="submit" id="login" value="Login" class="btn btn-primary btn-md"/><!-- <a href="#" class="btn btn-primary btn-md">login <i class="fa fa-sign-in"></i></a> -->
</span>
</div>
</div>
</div>
</div>
</form>
登录控制器
@RequestMapping("login")
public ModelAndView getLoginForm(
String authfailed,
String logout, String denied,HttpServletRequest request, HttpServletResponse response)
throws ClassNotFoundException, SQLException, UsernameNotFoundException, IOException {
String message = "";
if (authfailed != null) {
message = "Invalid username or password, try again !";
HttpSession session = request.getSession(true);
String uname = (String) session.getAttribute("username");
PrintWriter out = new PrintWriter(System.out);
//response.sendRedirect("/incidentManagementPortal/PasswordReset");
} else if (logout != null) {
request.getSession().invalidate();
eraseCookie(request,response);
message = "Logged Out successfully, login again to continue !";
} else if (denied != null) {
message = "Access denied for this user !";
}
return new ModelAndView("login", "message", message);
}
当身份验证失败时,它会使用上述方法,如果我正在使用 request.getParameter('username'); 我得到 NULL。
解决方案
它只是一个表单帖子,因此您可以从请求的参数中获取它。
默认情况下,使用 request.getParameter("j_username")。如果为参数“用户名”配置自定义名称,则使用 request.getParameter(“[custom_param_name]”)。
好的,所以您在 Spring 安全重定向回登录页面以失败身份验证后尝试获取用户名。您使用的是哪个 Spring Security 版本?如果在 3.1.0 之前,您可以从会话属性 SPRING_SECURITY_LAST_USERNAME 中获取它。如果没有,您可以实现 AuthenticationFailureHandler 将其缓存在会话中。详情见:返回用户名和密码登录表单 grails spring security
推荐阅读
- reactjs - 图像标签不显示图像
- java - 部署到 Tomcat 10 后无法访问 Spring Boot 应用程序
- android - 从 Android 设备上的网站下载 ssl 证书
- ios - XCODE Swift 5.0 框架问题 -ObjC 和 FacebookSDK
- firebase - 如何在 TypeScript 数据模型中使用 FieldValue ServerTimestamp?
- azerothcore - Player:GetAccountId() 传递了一个表参数
- python - 将数组作为列附加到数据框(或根据其他数据框的日期创建新的数据框)
- python - 尝试遍历数据框行但仅访问列标题
- json - 将 JSON 值展平为键
- javascript - 如何获取另一个 HTML 页面的元素