PermissionInterceptor.java 3.0 KB
Newer Older
M
MaxKey 已提交
1
/*
M
220413  
MaxKey 已提交
2
 * Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
M
MaxKey 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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 46 47 48 49 50 51
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 

package org.maxkey.authn.web.interceptor;

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

import org.maxkey.authn.SigninPrincipal;
import org.maxkey.authn.jwt.AuthJwtService;
import org.maxkey.authn.online.OnlineTicketService;
import org.maxkey.authn.web.AuthorizationUtils;
import org.maxkey.configuration.ApplicationConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.AsyncHandlerInterceptor;
/**
 * 权限Interceptor处理
 * @author Crystal.Sea
 *
 */
@Component
public class PermissionInterceptor  implements AsyncHandlerInterceptor  {
	private static final Logger _logger = LoggerFactory.getLogger(PermissionInterceptor.class);
	//无需Interceptor url
	@Autowired
	ApplicationConfig applicationConfig;
	
	@Autowired
	OnlineTicketService onlineTicketService;
	
	@Autowired
	AuthJwtService authJwtService ;
	
M
220413  
MaxKey 已提交
52 53
	boolean mgmt = false;
	
M
MaxKey 已提交
54 55 56 57 58 59 60 61 62
	/*
	 * 请求前处理
	 *  (non-Javadoc)
	 * @see org.springframework.web.servlet.handler.HandlerInterceptorAdapter#preHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)
	 */
	@Override
	public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {
		 _logger.trace("PermissionAdapter preHandle");
		 AuthorizationUtils.authenticate(request, authJwtService, onlineTicketService);
M
220413  
MaxKey 已提交
63 64 65
		 SigninPrincipal principal = AuthorizationUtils.getPrincipal();
		//判断用户是否登录,判断用户是否登录用户
		if(principal == null){
M
MaxKey 已提交
66 67 68 69 70 71
			_logger.trace("No Authentication ... forward to /auth/entrypoint");
			RequestDispatcher dispatcher = request.getRequestDispatcher("/auth/entrypoint");
		    dispatcher.forward(request, response);
		    return false;
		}
		
M
220413  
MaxKey 已提交
72 73
		//管理端必须使用管理员登录,非管理员用户直接注销
		if (this.mgmt && !principal.isRoleAdministrators()) {
M
MaxKey 已提交
74
		    _logger.debug("Not ADMINISTRATORS Authentication .");
M
220413  
MaxKey 已提交
75
		    RequestDispatcher dispatcher = request.getRequestDispatcher("/auth/entrypoint");
M
MaxKey 已提交
76 77 78 79
		    dispatcher.forward(request, response);
		    return false;
		}
		
M
220413  
MaxKey 已提交
80
		return true;
M
MaxKey 已提交
81
	}
M
220413  
MaxKey 已提交
82 83 84 85 86 87

	public void setMgmt(boolean mgmt) {
		this.mgmt = mgmt;
		_logger.debug("Permission for ADMINISTRATORS {}", this.mgmt);
	}
	
M
MaxKey 已提交
88
}