首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

struts2登录阻截

2013-11-01 
struts2登录拦截用户没有登录的情况下,如果要进行操作,则直接跳到登录页面先进行登录,想问一下各位大牛要

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 IOException, ServletException {
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 
------解决方案--------------------


 session.getAttribute("user")==null ) {
httpResponse.sendRedirect(httpRequest.getContextPath()
+ Constants.LOGIN);
return;
}
}
filterChain.doFilter(httpRequest, httpResponse);
}

@Override
public void init(FilterConfig arg0) throws ServletException {

}

}



web.xml:
<filter>
<filter-name>SecurityFilter</filter-name>
<filter-class>com.abacus.pace.web.action.SecurityFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SecurityFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<context-param>
<param-name>excludeURLlist</param-name>
<param-value>login.do,userLogin.do,logout.do</param-value>
</context-param>
[解决办法]
亲,你没有写拦截器哦,在这里给你推荐一篇博客你看看对你用没有帮助。
http://492664447-qq-com.iteye.com/blog/1272532

热点排行