Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.tc.services.security;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Bean;
- import org.springframework.security.authentication.AuthenticationServiceException;
- import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
- import org.springframework.security.core.Authentication;
- import org.springframework.security.core.AuthenticationException;
- import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
- import org.springframework.stereotype.Component;
- import org.springframework.web.servlet.FlashMap;
- import org.springframework.web.servlet.FlashMapManager;
- import org.springframework.web.servlet.support.RequestContextUtils;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.util.ArrayList;
- import java.util.List;
- public class LoginUsernamePasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
- private String usernameParameter = SPRING_SECURITY_FORM_USERNAME_KEY;
- private String passwordParameter = SPRING_SECURITY_FORM_PASSWORD_KEY;
- private boolean postOnly = true;
- @Override
- public Authentication attemptAuthentication(HttpServletRequest request,
- HttpServletResponse response) throws AuthenticationException {
- if (postOnly && !request.getMethod().equals("POST")) {
- throw new AuthenticationServiceException(
- "Authentication method not supported: " + request.getMethod());
- }
- String username = obtainUsername(request);
- String password = obtainPassword(request);
- FlashMap flashMap = new FlashMap();
- if (username == null) {
- username = "";
- flashMap.addTargetRequestParam
- ("loginError","Login must not be blank.");
- }
- if (password == "") {
- flashMap.addTargetRequestParam
- ("passwordError","Password must not be blank.");
- }
- FlashMapManager manager = RequestContextUtils
- .getFlashMapManager(request);
- manager.saveOutputFlashMap(flashMap,request,response);
- username = username.trim();
- UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
- username, password);
- // Allow subclasses to set the "details" property
- setDetails(request, authRequest);
- return this.getAuthenticationManager().authenticate(authRequest);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement