我的 login.jsp 上有一个自定义字段以及“ j_username
”和“ j_password
”,我需要对用户进行身份验证。我正在使用 CustomUsernamePasswordAuthenticationFilter 来访问自定义字段,如下所示。
public class CustomUsernamePasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) {
String myCustomField= request.getParameter("myCustomField");
request.getSession().setAttribute("CUSTOM_FIELD", myCustomField);
return super.attemptAuthentication(request, response);
}
}
我尝试在 UserDetailsService 类的方法中访问会话,loadByUsername
但出现错误。这是我的自定义 UserDetailsService 的代码。
public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException, DataAccessException {
ServletRequestAttributes attr = (ServletRequestAttributes)RequestContextHolder.currentRequestAttributes();
HttpSession session = attr.getRequest().getSession();
User userObject = dbObject.retrieveUser(userName,myCustomParameter)
// code here to retrieve my user from the DB using the userName and myCustomParameter that was retrieved from login.jsp and put in the session. Get the custom parameter from the session here.
if (userObject == null)
throw new UsernameNotFoundException("user not found");
return new AuthenticationUserDetails(userObject);
}
有什么方法可以访问此自定义参数进行身份验证?通过会话发送它似乎不起作用。