Authored by mali

登录

package com.yoho.ufo.constants;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
* @author ping.huang
* 2016年7月25日
*/
public enum CodeEnum {
SUCCESS(200, "成功"),
ERROR(201, "失败"),
DATABASE_TIME_OUT(900, "数据库查询超时"),
SESSION_TIME_OUT(901, "登录超时")
;
private int code;
private String message;
private CodeEnum(int code, String message) {
this.code = code;
this.message = message;
}
public static String getMessageByCode(int code) {
for (CodeEnum e : values()) {
if (code == e.getCode()) {
return e.getMessage();
}
}
return null;
}
/**
* 返回json格式的数据
* @param value
* @return
*/
public static JSONArray toJSON(int value) {
JSONArray arr = new JSONArray(10);
JSONObject json = null;
for (CodeEnum e : values()) {
json = new JSONObject();
json.put("value", e.getClass());
json.put("text", e.getMessage());
arr.add(json);
}
return arr;
}
public int getCode() {
return code;
}
public String getMessage() {
return message;
}
}
\ No newline at end of file
... ...
package com.yoho.ufo.interceptor;
/**
* Created by li.ma on 2018/9/13.
*/
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.yoho.ufo.constants.CodeEnum;
import com.yoho.ufo.constants.PlatformConstant;
import com.yoho.ufo.login.model.OperatorBean;
import com.yoho.ufo.login.model.UserInfoResponseBO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
/**
* 描述:
* 做url权限校验
* @author ping.huang
* 2016年4月11日
*/
public class UrlInterceptor implements HandlerInterceptor {
private static final Logger log = LoggerFactory.getLogger(UrlInterceptor.class);
//免过滤列表(不管有没有session都可以访问)
private List<String> firstNoFilterList = new ArrayList<String>();
//第二级免过滤列表(只要有session就都可以访问)
private List<String> secondNoFilterList = new ArrayList<String>();
private static final ThreadLocal<OperatorBean> localOpera = new ThreadLocal<>();
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
String url = (String) request.getAttribute("javax.servlet.include.request_uri");
if (StringUtils.isEmpty(url)) {
url = request.getRequestURI();
}
log.info("url is {}.",url);
OperatorBean opera = new OperatorBean();
opera.setClientIp(request.getRemoteAddr());
localOpera.set(opera);
String lastName = url.substring(url.lastIndexOf("/") + 1);
log.info("lastName is {}.",lastName);
/*//只拦截*.do
if (!lastName.endsWith(".do")) {
return true;
}*/
//第一层免过滤
if (StringUtils.isEmpty(lastName) || firstNoFilterList.contains(lastName)) {
return true;
}
//过滤session失效的
//UserInfoResponseBO user = loginSessionUtil.getLoginSession(request);
UserInfoResponseBO user = (UserInfoResponseBO) request.getSession().getAttribute(PlatformConstant.USER_SESSION);
if (user == null) {
log.warn("session 过期,重新登录,lastName is {}.",lastName);
response.setStatus(CodeEnum.SESSION_TIME_OUT.getCode());
return false;
}
opera.setUser(user);
return true;
}
public static OperatorBean getLocalOpera(){
return localOpera.get();
}
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception {
localOpera.remove();
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception {
// TODO Auto-generated method stub
}
public void setFirstNoFilterList(List<String> firstNoFilterList) {
this.firstNoFilterList = firstNoFilterList;
}
public void setSecondNoFilterList(List<String> secondNoFilterList) {
this.secondNoFilterList = secondNoFilterList;
}
}
... ...
package com.yoho.ufo.login.model;
import com.alibaba.fastjson.JSONObject;
/**
* @author ping.huang
* 2016年8月31日
*/
public class OperatorBean {
private static final long serialVersionUID = -5982639168101762749L;
private String clientIp;
private UserInfoResponseBO user;
private boolean superUser;
public String getClientIp() {
return clientIp;
}
public void setClientIp(String clientIp) {
this.clientIp = clientIp;
}
public UserInfoResponseBO getUser() {
return user;
}
public void setUser(UserInfoResponseBO user) {
this.user = user;
}
public boolean isSuperUser() {
return superUser;
}
public void setSuperUser(boolean superUser) {
this.superUser = superUser;
}
@Override
public String toString() {
return JSONObject.toJSONString(this);
}
}
\ No newline at end of file
... ...
... ... @@ -22,16 +22,13 @@
<mvc:resources mapping="/js/**" location="/js/" />
<!--<bean id="urlInterceptor" class="com.yohobuy.platform.common.interceptor.UrlInterceptor">
&lt;!&ndash; 一级免过滤列表(不需要登录,就可以访问的url) &ndash;&gt;
<bean id="urlInterceptor" class="com.yoho.ufo.interceptor.UrlInterceptor">
<!--&lt;!&ndash; 一级免过滤列表(不需要登录,就可以访问的url) &ndash;&gt;-->
<property name="firstNoFilterList">
<list>
<value>loginForPid.do</value>
<value>platformLogin.do</value>
<value>queryBrands.do</value>
<value>removeLoginUser.do</value>
<value>logout.do</value>
<value>addChannelProduct.do</value>
<!--<value>addChannelProduct.do</value>
<value>addChannelProductByBrand.do</value>
<value>selectAll.do</value>
<value>synChannelProduct</value>
... ... @@ -41,26 +38,21 @@
<value>repairShopNames</value>
<value>backList.do</value>
<value>channelProductTmpSync.do</value>
<value>export.do</value>
<value>export.do</value>-->
</list>
</property>
&lt;!&ndash; 只要登录,就能访问的url &ndash;&gt;
<!--&lt;!&ndash; 只要登录,就能访问的url &ndash;&gt;-->
<property name="secondNoFilterList">
<list>
<value>NoFilterController.do</value>
</list>
</property>
<property name="superManagerList">
<list>
<value>admin</value>
</list>
</property>
</bean>
<mvc:interceptors>
<ref bean="urlInterceptor" />
<ref bean="threadProfileInterceptor"/>
</mvc:interceptors>-->
</mvc:interceptors>
<bean id="requestFactory" class="org.springframework.http.client.SimpleClientHttpRequestFactory">
<property name="readTimeout" value="60000"/>
... ...