1. 程式人生 > >SSH:攔截器簡單應用

SSH:攔截器簡單應用

此例子的攔截器目的防止使用者不登入通過攔截器直接獲取伺服器的資源(服務不願意的,嚶嚶嚶)

將secret.jsp作為機密檔案放在WEB-INF下面,WEB-INF為使用者無法訪問頁面。

登入首頁

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	${tip}
	<s:form action="login2" method="post">
		<s:textfield key="使用者名稱" name="username" />
		<s:textfield key="姓名" name="password" />
		<s:submit value="登入" />
	</s:form>
</body>
</html>

首頁對應的Action

package Test;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction2 extends ActionSupport {
	private String username;
	private String password;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String execute() throws Exception {
		String strReturn = INPUT;
		if (this.username.equals("abc") && this.password.equals("123")) {
			ActionContext.getContext().getSession().put("username", username);
			strReturn = SUCCESS;
		} else {
			ActionContext.getContext().getSession().put("tip", "登入失敗");
		}
		return strReturn;
	}
}

登入成功頁面

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<a href="secret">檢視機密資訊 </a>
</body>
</html>

登入成功對應Action

package Test;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class AuthInterceptor extends AbstractInterceptor {

	@Override
	public String intercept(ActionInvocation arg0) throws Exception {
		System.out.println("攔截器開始執行");
		Object obj = ActionContext.getContext().getSession().get("username");
		String strName = obj != null ? obj.toString() : "";
		if (strName.equals("abc")) {
			System.out.println("攔截器執行結束");
			return "success";
		} else {
			ActionContext.getContext().getSession().put("tip", "您未登入,還不能檢視機密資訊");
			return "input";
		}
	}

}

機密檔案:放在WEB-INF,WEB-INF裡面的檔案使用者無法通過瀏覽器訪問

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>這是機密資訊,一般不讓人看
</body>
</html>

struts2.xml配置

<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
	"http://struts.apache.org/dtds/struts-2.1.dtd">
	<struts>
		<package name="myPackage" extends="struts-default">
			<interceptors>
				<interceptor name="authInter" class="Test.AuthInterceptor"/>
			</interceptors>
			<action name="login2" class="Test.UserAction2">
				<result>success.jsp</result>
				<result name="input">login2.jsp</result>
			</action>
			<action name="secret" class="Test.UserAction2">
				<interceptor-ref name="authInter"/>
				<interceptor-ref name="defaultStack"/>
				<result name="success">WEB-INF/secret.jsp</result>
				<result name="input">/login2.jsp</result>
			</action>
		</package>
	</struts>