LoginEndpoint.java 3.1 KB
Newer Older
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
1 2 3 4 5
package org.maxkey.web.endpoint;

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

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
6 7
import org.maxkey.authn.BasicAuthentication;
import org.maxkey.authn.RealmAuthenticationProvider;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
8 9 10 11 12 13 14 15 16 17 18
import org.maxkey.authn.support.jwt.JwtLoginService;
import org.maxkey.authn.support.rememberme.AbstractRemeberMeService;
import org.maxkey.config.ApplicationConfig;
import org.maxkey.web.WebConstants;
import org.maxkey.web.WebContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
19
import org.springframework.web.bind.annotation.ModelAttribute;
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


/**
 * @author Crystal.Sea
 *
 */
@Controller
public class LoginEndpoint {
	private static Logger _logger = LoggerFactory.getLogger(LoginEndpoint.class);
	
	@Autowired
  	@Qualifier("applicationConfig")
  	protected ApplicationConfig applicationConfig;
 	
	
	@Autowired
	@Qualifier("remeberMeService")
	protected AbstractRemeberMeService remeberMeService;
	
	@Autowired
	@Qualifier("jwtLoginService")
	JwtLoginService jwtLoginService;
	
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
46 47 48 49
	@Autowired
	@Qualifier("authenticationProvider")
	RealmAuthenticationProvider authenticationProvider ;
	
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
	/**
	 * init login
	 * @return
	 */
 	@RequestMapping(value={"/login"})
	public ModelAndView login(
			HttpServletRequest request,
			HttpServletResponse response,
			@CookieValue(value=WebConstants.REMEBER_ME_COOKIE,required=false) String remeberMe,
			@RequestParam(value = WebConstants.JWT_TOKEN_PARAMETER, required = false) String jwt) {
 		
		_logger.debug("LoginController /login.");
		ModelAndView modelAndView = new ModelAndView();
		
		boolean isAuthenticated= WebContext.isAuthenticated();
		
		//for jwt Login
		if(!isAuthenticated){
			if(jwt!=null&&!jwt.equals("")){
				isAuthenticated=jwtLoginService.login(jwt, response);
			}
		}
				
		//for RemeberMe login
		if(!isAuthenticated){
			if(applicationConfig.getLoginConfig().isRemeberMe()&&remeberMe!=null&& !remeberMe.equals("")){
				isAuthenticated=remeberMeService.login(remeberMe,response);
			}
		}

		//for normal login
		if(!isAuthenticated){
			modelAndView.addObject("isRemeberMe", applicationConfig.getLoginConfig().isRemeberMe());
			
			modelAndView.addObject("isCaptcha", applicationConfig.getLoginConfig().isCaptcha());
			modelAndView.addObject("sessionid", WebContext.getSession().getId());
		}
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
87
		
MaxKey单点登录官方's avatar
mgt sso  
MaxKey单点登录官方 已提交
88 89 90
		if(WebContext.isAuthenticated()){
 			return WebContext.redirect("/main");
		}
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
91 92 93
		modelAndView.setViewName("login");
		return modelAndView;
	}
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
94 95 96 97 98 99 100
 	
 	@RequestMapping(value={"/logon.do"})
	public ModelAndView logon(@ModelAttribute("authentication") BasicAuthentication authentication) {
 		
 		if(WebContext.isAuthenticated()){
 			return WebContext.redirect("/main");
		}else{
MaxKey单点登录官方's avatar
mgt sso  
MaxKey单点登录官方 已提交
101
			authenticationProvider.authenticate(authentication);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
102 103 104
			return WebContext.redirect("/login");
		}
 	}
MaxKey单点登录官方's avatar
init  
MaxKey单点登录官方 已提交
105
}