当前位置: 代码迷 >> Java Web开发 >> struts2登录阻截
  详细解决方案

struts2登录阻截

热度:8346   发布时间:2016-04-10 23:59:04.0
struts2登录拦截
用户没有登录的情况下,如果要进行操作,则直接跳到登录页面先进行登录,想问一下各位大牛要怎么写?求代码

------解决方案--------------------
1.创建拦截器类,需要继承AbstractInterceptor,需要重写intercept方法。在intercept方法里面判断用户是否已登录(主要是在session里面取用户信息),然后做相应的页面导向。
2.在struts中配置刚才的拦截器类,然后与default拦截器组成拦截器组,然后设置该拦截器组为默认拦截器。搞定!
------解决方案--------------------

public class CheckLoginInterceptor implements Interceptor {

private String sessionAttribute;
private String reloginResult;

public void setSessionAttribute(String sessionAttribute) {
this.sessionAttribute = sessionAttribute;
}

public void setReloginResult(String reloginResult) {
this.reloginResult = reloginResult;
}

@Override
public void destroy() {
// TODO Auto-generated method stub

}

@Override
public void init() {
// TODO Auto-generated method stub

}

@SuppressWarnings("rawtypes")
@Override
public String intercept(ActionInvocation invocation) throws Exception {
// 读取session
Map session = invocation.getInvocationContext().getSession();
// 判断session中是否有相应的attribute
if (session.containsKey(sessionAttribute)) {
String resultCode = invocation.invoke();
return resultCode;
} else {
return reloginResult;
}

}

}


然后在struts.xml中引入即可
------解决方案--------------------

package org.ohshit.common.interceptors;

import java.util.Map;


import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;


import org.springframework.stereotype.Component;


import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
/**
 * 权限控制拦截器   对用户信息 进行查看 或者修改的操作  都将被拦截
 * @author Tone
 *
 */
@Component("OhShitPowerInterceptor")
public class OhShitPowerInterceptor extends AbstractInterceptor {


private static final long serialVersionUID = 1L;

@Override
public String intercept(ActionInvocation actioninvocation) throws Exception {

        HttpServletRequest request = ServletActionContext.getRequest();
        
        String url = request.getRequestURI();
//System.out.println("url:"+url);

Map<String,Object> session = actioninvocation.getInvocationContext().getSession();


if (url.indexOf("UserInfoAction")!=-1) {
if (session.get("LOGIN_USER")==null) {
request.setAttribute("nomsg", "请先登录");
return "login";
}
}
return actioninvocation.invoke();

}

}


------解决方案--------------------
刚学JAVA,防止用户跳过登录页面直接访问其他页面我是用filter做的:
在web.xml文件中添加<filter>,写一个类实现Filter。在doFilter方法中进行判断。
package com.abacus.pace.web.action;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.abacus.pace.common.Constants;

public class SecurityFilter implements Filter {

@Override
public void destroy() {

}

@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOExceptionServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;

String excludeList = httpRequest.getSession().getServletContext()
.getInitParameter(Constants.EXCLUDEURL);

String currentURL = httpRequest.getRequestURI();


String targetURL = currentURL.substring(
currentURL.lastIndexOf('/') + 1, currentURL.indexOf(';')==-1?currentURL.length():currentURL.indexOf(';')-1);
HttpSession session = httpRequest.getSession(false);

if (!excludeList.contains(targetURL)) {
if (session == null 
------解决方案--------------------
  相关解决方案