parent
d58c2d0a19
commit
d08abc8c7e
7 changed files with 297 additions and 1 deletions
@ -0,0 +1,35 @@ |
|||||||
|
package org.springblade.system.jinhong; |
||||||
|
|
||||||
|
import java.io.BufferedReader; |
||||||
|
import java.io.IOException; |
||||||
|
import java.io.InputStream; |
||||||
|
import java.io.InputStreamReader; |
||||||
|
import java.util.Properties; |
||||||
|
|
||||||
|
public class BaseUtils |
||||||
|
{ |
||||||
|
/** |
||||||
|
* 获取配置文件中的配置信息 |
||||||
|
* |
||||||
|
* @param key配置文件key值 |
||||||
|
* @return |
||||||
|
* @throws IOException |
||||||
|
*/ |
||||||
|
public static String getConfigValue(String key) |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
Properties props = new Properties(); |
||||||
|
InputStream in = BaseUtils.class.getClassLoader() |
||||||
|
.getResourceAsStream("config.properties"); |
||||||
|
BufferedReader bf = new BufferedReader(new InputStreamReader(in, |
||||||
|
"UTF-8")); |
||||||
|
props.load(bf); |
||||||
|
return props.getProperty(key); |
||||||
|
} |
||||||
|
catch (IOException ex) |
||||||
|
{ |
||||||
|
return ""; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,156 @@ |
|||||||
|
package org.springblade.system.jinhong; |
||||||
|
|
||||||
|
import java.net.URLEncoder; |
||||||
|
import java.util.Iterator; |
||||||
|
import java.util.Map.Entry; |
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest; |
||||||
|
import javax.servlet.http.HttpServletResponse; |
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j; |
||||||
|
import org.springframework.stereotype.Component; |
||||||
|
import org.springframework.web.servlet.ModelAndView; |
||||||
|
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; |
||||||
|
|
||||||
|
import com.yawei.pso.PSORequest; |
||||||
|
import com.yawei.pso.SSOResponse; |
||||||
|
import com.yawei.pso.TicketManager; |
||||||
|
|
||||||
|
/** |
||||||
|
* 自定义拦截器,拦截符合条件url的请求 身份验证拦截 |
||||||
|
* (是否有登录用户信息) |
||||||
|
*/ |
||||||
|
@Slf4j |
||||||
|
@Component |
||||||
|
public class IdentityInterceptor extends HandlerInterceptorAdapter |
||||||
|
{ |
||||||
|
|
||||||
|
// 读取ssoToken参数
|
||||||
|
private static final String strToken = BaseUtils.getConfigValue("ssoKey"); |
||||||
|
|
||||||
|
public final static String SEESION_USER = "seesion_user"; |
||||||
|
|
||||||
|
/** |
||||||
|
* 在业务处理器处理请求之前被调用 如果返回false 从当前的拦截器往回执行所有拦截器的afterCompletion(),再退出拦截器链 |
||||||
|
* 如果返回true 执行下一个拦截器,直到所有的拦截器都执行完毕 再执行被拦截的Controller 然后进入拦截器链, |
||||||
|
* 从最后一个拦截器往回执行所有的postHandle() 接着再从最后一个拦截器往回执行所有的afterCompletion() |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public boolean preHandle(HttpServletRequest request, |
||||||
|
HttpServletResponse response, Object handler) throws Exception |
||||||
|
{ |
||||||
|
|
||||||
|
log.debug("==============执行顺序: 1、preHandle================"); |
||||||
|
// 获取当前请求的url
|
||||||
|
String requestUri = request.getRequestURI(); |
||||||
|
|
||||||
|
Validator validator = Validator.getInstance(); |
||||||
|
// 注入当前session
|
||||||
|
// validator.init(request);
|
||||||
|
|
||||||
|
String strResponse = request.getParameter(strToken); |
||||||
|
|
||||||
|
//真实环境需要放开如下注释
|
||||||
|
/** |
||||||
|
* |
||||||
|
|
||||||
|
if (strResponse != null) |
||||||
|
{ |
||||||
|
// 如果服务器端通过认证后,会返回后执行改操作,然后写入cookie
|
||||||
|
SSOResponse ssoResp = new SSOResponse(strResponse); |
||||||
|
TicketManager tm = ssoResp.CreatePSOTicket(); |
||||||
|
if (tm == null) |
||||||
|
{ |
||||||
|
PSORequest psoRequest = new PSORequest(request); |
||||||
|
String requeststr = psoRequest.CreateHash(); |
||||||
|
|
||||||
|
String keeperUrl = BaseUtils.getConfigValue("keeperUrl"); |
||||||
|
keeperUrl = keeperUrl + "?" + strToken + "=" |
||||||
|
+ URLEncoder.encode(requeststr, "UTF-8"); |
||||||
|
response.sendRedirect(keeperUrl); |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
String domainName = BaseUtils.getConfigValue("domain"); |
||||||
|
tm.SaveTicket(response, domainName); |
||||||
|
|
||||||
|
Iterator<Entry<String, String[]>> iterator = request |
||||||
|
.getParameterMap().entrySet().iterator(); |
||||||
|
StringBuffer param = new StringBuffer(); |
||||||
|
int i = 0; |
||||||
|
while (iterator.hasNext()) |
||||||
|
{ |
||||||
|
Entry<String, String[]> entry = (Entry<String, String[]>) iterator |
||||||
|
.next(); |
||||||
|
if (entry.getKey().equals(strToken)) |
||||||
|
continue; |
||||||
|
else |
||||||
|
{ |
||||||
|
i++; |
||||||
|
if (i == 1) |
||||||
|
param.append("?").append(entry.getKey()) |
||||||
|
.append("="); |
||||||
|
else |
||||||
|
param.append("&").append(entry.getKey()) |
||||||
|
.append("="); |
||||||
|
|
||||||
|
if (entry.getValue() instanceof String[]) |
||||||
|
{ |
||||||
|
param.append(((String[]) entry.getValue())[0]); |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
param.append(entry.getValue()); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
response.sendRedirect(requestUri + param.toString()); |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
TicketManager tm = new TicketManager(); |
||||||
|
if (!tm.LoadTicket(request)) |
||||||
|
{ |
||||||
|
PSORequest psoRequest = new PSORequest(request); |
||||||
|
String requeststr = psoRequest.CreateHash(); |
||||||
|
|
||||||
|
String keeperUrl = BaseUtils.getConfigValue("keeperUrl"); |
||||||
|
keeperUrl = keeperUrl + "?" + strToken + "=" |
||||||
|
+ URLEncoder.encode(requeststr, "UTF-8"); |
||||||
|
response.sendRedirect(keeperUrl); |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
validator.SetUserTicket(request); |
||||||
|
return true; |
||||||
|
*/ |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 在业务处理器处理请求执行完成后,生成视图之前执行的动作 可在modelAndView中加入数据,比如当前时间 |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public void postHandle(HttpServletRequest request, |
||||||
|
HttpServletResponse response, Object handler, |
||||||
|
ModelAndView modelAndView) throws Exception |
||||||
|
{ |
||||||
|
log.debug("==============执行顺序: 2、postHandle================"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 在DispatcherServlet完全处理完请求后被调用,可用于清理资源等 |
||||||
|
* |
||||||
|
* 当有拦截器抛出异常时,会从当前拦截器往回执行所有的拦截器的afterCompletion() |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public void afterCompletion(HttpServletRequest request, |
||||||
|
HttpServletResponse response, Object handler, Exception ex) |
||||||
|
throws Exception |
||||||
|
{ |
||||||
|
log.debug("==============执行顺序: 3、afterCompletion================"); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,90 @@ |
|||||||
|
package org.springblade.system.jinhong; |
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest; |
||||||
|
import javax.servlet.http.HttpSession; |
||||||
|
|
||||||
|
import com.yawei.pso.TicketManager; |
||||||
|
|
||||||
|
/** |
||||||
|
* 验证器 |
||||||
|
*/ |
||||||
|
public class Validator |
||||||
|
{ |
||||||
|
private static ThreadLocal<Validator> validatorHolder = new ThreadLocal<Validator>() |
||||||
|
{ |
||||||
|
|
||||||
|
protected Validator initialValue() |
||||||
|
{ |
||||||
|
return new Validator(); |
||||||
|
} |
||||||
|
|
||||||
|
}; |
||||||
|
|
||||||
|
// 当前请求的session
|
||||||
|
private HttpSession session = null; |
||||||
|
|
||||||
|
// 当前的请求
|
||||||
|
private HttpServletRequest request = null; |
||||||
|
|
||||||
|
private Validator() |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
public static Validator getInstance() |
||||||
|
{ |
||||||
|
return validatorHolder.get(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 执行初始化 |
||||||
|
* @param httpRequest |
||||||
|
*/ |
||||||
|
public void init(HttpServletRequest httpRequest) |
||||||
|
{ |
||||||
|
this.request = httpRequest; |
||||||
|
this.session = request.getSession(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 将凭证身份加入到session |
||||||
|
* @param httpRequest |
||||||
|
*/ |
||||||
|
public void SetUserTicket(HttpServletRequest httpRequest) |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
if (httpRequest.getSession() |
||||||
|
.getAttribute(IdentityInterceptor.SEESION_USER) == null) |
||||||
|
{ |
||||||
|
TicketManager ticket = new TicketManager(); |
||||||
|
if (ticket.LoadTicket(httpRequest)) |
||||||
|
{ |
||||||
|
// 登录用户姓名
|
||||||
|
String userName = ticket.getUserName(); |
||||||
|
// 登录用户账号
|
||||||
|
String userAccount = ticket.getUserID(); |
||||||
|
// 登录用户标识
|
||||||
|
String userGuid = ticket.getADGUID(); |
||||||
|
System.out.println("===userName===" + userName); |
||||||
|
System.out.println("===userAccount===" + userAccount); |
||||||
|
System.out.println("===userGuid===" + userGuid); |
||||||
|
} |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
} |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 清除session |
||||||
|
*/ |
||||||
|
public void cancel() |
||||||
|
{ |
||||||
|
this.session = null; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,5 @@ |
|||||||
|
#系统配置信息 |
||||||
|
#认证配置 |
||||||
|
domain=yw.com.cn |
||||||
|
ssoKey=SSOToken |
||||||
|
keeperUrl=http://localhost:8088/Keeper.aspx |
||||||
Loading…
Reference in new issue