标题:
sturts配置登陆拦截器(2)
[打印本页]
作者:
look_w
时间:
2019-4-20 14:30
标题:
sturts配置登陆拦截器(2)
spring.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 指定Spring配置文件的Schema信息 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/s ... ing-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
">
<bean id="queryAction" class="action.QueryAction">
<property name="resultTxtReposity">
<ref bean="resultTxtReposity" />
</property>
</bean>
<bean id="login" class="action.LoginAction">
<property name="userReposity">
<ref bean="userReposity" />
</property>
</bean>
<bean id = "authorityBean" class="action.core.LoginInterceptor">
<property name="userReposity">
<ref bean="userReposity" />
</property>
</bean>
</beans>
LoginInterceptor.java
package action.core;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import reposity.UserReposity;
import action.LoginAction;
import action.service.UserService;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import entity.User;
@SuppressWarnings("serial")
public class LoginInterceptor extends AbstractInterceptor {
private UserReposity userReposity;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
HttpSession session = ServletActionContext.getRequest().getSession();
Cookie[] cookies = ServletActionContext.getRequest().getCookies();
//session判断当前用户是否登陆
if(session.getAttribute("uflag") != null &&
LoginAction.ISLOGIN == Integer.valueOf(session.getAttribute("uflag").toString())){
return invocation.invoke();
}
if(cookies == null) return invocation.invoke();
//获取cookie
String username = null, password = null, rememberme = null;
for (Cookie cookie : cookies) {
if(cookie.getName().equals("user")){
username = cookie.getValue();
}
if(cookie.getName().equals("cookie")){
password = cookie.getValue();
}
if(cookie.getName().equals("rememberme")){
rememberme = cookie.getValue();
}
}
if(username == null || password == null || !LoginAction.REMEMBER_ME.equals(rememberme)){
return Action.LOGIN;
}
//验证cookie的有效性
User user = UserService.findUser(userReposity, username, password);
if(user != null){
session.setAttribute("uflag",1);
session.setAttribute("acount",user);
return invocation.invoke();
}
return Action.LOGIN;
}
public UserReposity getUserReposity() {
return userReposity;
}
public void setUserReposity(UserReposity userReposity) {
this.userReposity = userReposity;
}
}
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0