可能是因為密碼不正確,特別是密碼使用md5或者其它的加密算法加密之后,更是如此。
還有一點:
在UsernamePasswordAuthenticationFilter的子類中的attemptAuthentication方法中,只要去驗證你自己的邏輯就可以了,不要在這里驗證用戶名,密碼是否正確,因為這是UserDetailsService要干的事情。
比如我的attemptAuthentication方法中就是這樣:
@Override public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException { if (!request.getMethod().equals("POST")) { throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod()); } //檢測驗證碼 checkValidateCode(request); String username = obtainUsername(request); String password = obtainPassword(request); //為了辨別從前台進入的,還是從后台進入的 String type=request.getParameter(ENTRY); request.getSession().setAttribute(USER_ENTRY, type); if("bg".equals(type)) { username="bg_"+username; }else if("fg".equals(type)) { username="fg_"+username; } //這里的username會傳給UserDetailsService的loadUserByUsername方法,作為loadUserByUsername的參數。 UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password); // 允許子類設置詳細屬性 setDetails(request, authRequest); // 運行UserDetailsService的loadUserByUsername 再次封裝Authentication AuthenticationManager authenticationManager = this.getAuthenticationManager(); Authentication authentication=authenticationManager.authenticate(authRequest); return authentication; }
