package com.ndpmedia.rocketmq.cockpit.controller;

import com.ndpmedia.rocketmq.authentication.RocketMQUserLoginService;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
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.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * the login controller.
 */
@Controller
@RequestMapping(value = "/login")
public class LoginController extends UsernamePasswordAuthenticationFilter
{
    private RocketMQUserLoginService rocketMQUserLoginService;

    @RequestMapping(value = "/" ,method = {RequestMethod.GET , RequestMethod.POST})
    public String showIndex() {
        return "login";
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
            throws AuthenticationException
    {
        if(!request.getMethod().equals("POST")) {
            throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
        } else {
            String username = this.obtainUsername(request);
            String password = this.obtainPassword(request);

            if(username == null) {
                username = "";
            }

            if(password == null) {
                password = "";
            }

            boolean status = rocketMQUserLoginService.findUserStatus(username);

            if (!status)
            {
                password = "";
            }

            username = username.trim();
            UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
            this.setDetails(request, authRequest);
            return this.getAuthenticationManager().authenticate(authRequest);
        }
    }

    public RocketMQUserLoginService getRocketMQUserLoginService()
    {
        return rocketMQUserLoginService;
    }

    public void setRocketMQUserLoginService(RocketMQUserLoginService rocketMQUserLoginService)
    {
        this.rocketMQUserLoginService = rocketMQUserLoginService;
    }
}

@@ -118,451 +118,8 @@
 ce;%0A
-import org.springframework.security.authentication.AuthenticationManager;%0Aimport org.springframework.security.authentication.AuthenticationProvider;%0Aimport org.springframework.security.authentication.AuthenticationServiceException;%0Aimport org.springframework.security.authentication.UsernamePasswordAuthenticationToken;%0Aimport org.springframework.security.core.Authentication;%0Aimport org.springframework.security.core.AuthenticationException;%0A
 impo
@@ -203,24 +203,24 @@
 tionFilter;%0A
+
 import org.s
@@ -387,136 +387,8 @@
 d;%0A%0A
-import javax.annotation.Resource;%0Aimport javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpServletResponse;%0A%0A
 /**%0A
@@ -494,53 +494,8 @@
 ller
- extends UsernamePasswordAuthenticationFilter
 %0A%7B%0A 
@@ -669,24 +669,24 @@
 owIndex() %7B%0A
+
         retu
@@ -708,1119 +708,8 @@
  %7D%0A%0A
-    @Override%0A    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)%0A            throws AuthenticationException%0A    %7B%0A        if(!request.getMethod().equals(%22POST%22)) %7B%0A            throw new AuthenticationServiceException(%22Authentication method not supported: %22 + request.getMethod());%0A        %7D else %7B%0A            String username = this.obtainUsername(request);%0A            String password = this.obtainPassword(request);%0A%0A            if(username == null) %7B%0A                username = %22%22;%0A            %7D%0A%0A            if(password == null) %7B%0A                password = %22%22;%0A            %7D%0A%0A            boolean status = rocketMQUserLoginService.findUserStatus(username);%0A%0A            if (!status)%0A            %7B%0A                password = %22%22;%0A            %7D%0A%0A            username = username.trim();%0A            UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);%0A            this.setDetails(request, authRequest);%0A            return this.getAuthenticationManager().authenticate(authRequest);%0A        %7D%0A    %7D%0A%0A
     

remove extends relationship.