Struts2中利用filter、session實(shí)現(xiàn)安全訪問和身份認(rèn)證_第1頁(yè)
Struts2中利用filter、session實(shí)現(xiàn)安全訪問和身份認(rèn)證_第2頁(yè)
Struts2中利用filter、session實(shí)現(xiàn)安全訪問和身份認(rèn)證_第3頁(yè)
Struts2中利用filter、session實(shí)現(xiàn)安全訪問和身份認(rèn)證_第4頁(yè)
Struts2中利用filter、session實(shí)現(xiàn)安全訪問和身份認(rèn)證_第5頁(yè)
已閱讀5頁(yè),還剩9頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、Struts2 中利用中利用 filter、session 實(shí)現(xiàn)安全訪問和身份認(rèn)證實(shí)現(xiàn)安全訪問和身份認(rèn)證來(lái)自原博客鏈接: http:/ 軟件JDK 1.7Apach Tomcat 72、通過 eclipse 創(chuàng)建 Dynamic Web Project 后,導(dǎo)入相應(yīng)的 Struts2 的 jar 文件:3、導(dǎo)入 jar 包后,創(chuàng)建如下圖所示項(xiàng)目相應(yīng)目錄:權(quán)限說明(1) 根目錄(WebContent)下的資源,如:index.jsp 和 login.jsp,允許匿名訪問。(2)Admin目錄下的admin.jsp只允許角色為”admin”的用戶訪問。User目錄下的user.jsp只允許角色為”

2、user”的用戶訪問4、相應(yīng)的 jsp 代碼如下:index.jsp:html view plain copy.6.7.Insert title here1.12.13.welcome to you !0.21.login.jsp:html view plain copy3.base href=14.15.Insert title here9.20.21.用戶名5.密碼8.user.

3、jsp:html view plain copy.6.7.Insert title here9.用戶名:3.余額:7.住址:1.電話:5.36.37.admin.jsp:html view plain copy.6.7.Insert title here9.用戶名:3.余額:7.住址:1.電話:5.36.37.創(chuàng)建用于登陸驗(yàn)證類 Login.

4、java:java view plain copy1.package com.axb.cheney.filter;2.3.4.import javax.servlet.http.HttpServletRequest;5.import javax.servlet.http.HttpSession;6.7.import erceptor.ServletRequestAware;8.9.import com.opensymphony.xwork2.ActionSupport;10.11.public class Login extends ActionSu

5、pport12.implements ServletRequestAware13.14.private static final long serialVersionUID = 1L;15.private String name;16.private String password;17.private HttpServletRequest request;18.19.public String pass()20.21.HttpServletRequest req = this.request;22.HttpSession session = req.getSession();23.if (t

6、.equals(user1) & (this.password.equals(password1)24.session.setAttribute(name, );25.session.setAttribute(balance, 10,000);26.session.setAttribute(address, 廣東省深圳市福田區(qū)購(gòu)物公園);27.session.setAttribute(tel, 12665654856);28.System.out.println(login: + );29.return user;30.if (thi

7、.equals(admin) & (this.password.equals(password2)31.session.setAttribute(name, );32.session.setAttribute(balance, 9,000);33.session.setAttribute(address, 廣東省珠海市香洲區(qū)北理工);34.session.setAttribute(tel,;35.System.out.println(login: + );36.return admin;37.38.System.

8、out.println(login: fail);39.return failure;40.41.42.public String getName()43.44.return ;45.46.47.public void setName(String name) 48. = name;49.50.51.public String getPassword() 52.return this.password;53.54.55.public void setPassword(String password) 56.this.password = password;5

9、7.58.59.public HttpServletRequest getRequest() 60.return this.request;61.62.63.public void setServletRequest(HttpServletRequest request)64.65.this.request = request;66.67.修改 Struts.xml 文件:html view plain copy.6./WEB-INF/error.jsp17.18.19./login.jsp 20.21.23./login.jsp

10、 24./user/user.jsp 25./admin/admin.jsp 9.創(chuàng)建用于攔截驗(yàn)證身份的 UserAuthenticationFilter.javajava view plain copy1.package com.axb.cheney.filter;2.3.import java.io.IOException;4.5.import javax.servlet.Filter;6.import javax.servlet.FilterChain;7.import javax.servlet.FilterConfig;8.import javax.servlet

11、.ServletException;9.import javax.servlet.ServletRequest;10.import javax.servlet.ServletResponse;11.import javax.servlet.http.HttpServletRequest;12.import javax.servlet.http.HttpServletResponse;13.import javax.servlet.http.HttpSession;14.15.public class UserAuthenticationFilter16.implements Filter17.

12、18.private static String LOGIN_PAGE = /login.jsp;19.20.public void destroy()4.public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)25.throws IOException, ServletException26.27.HttpServletRequest req = (HttpServletRequest)request;28.HttpServletResponse re

13、s = (HttpServletResponse)response;29.30.String currentUrl = req.getServletPath();31.32.HttpSession session = req.getSession();33.34.System.out.println(UserAuthenticationFilter);35.if (currentUrl.equals() currentUrl = currentUrl + /;36.if (currentUrl.startsWith(/) & (!currentUrl.startsWith(/login

14、.jsp) 37.String user = (String)session.getAttribute(name);38.if (user = null) 39.res.sendRedirect(req.getContextPath() + LOGIN_PAGE);40.return;41.42.if (!user.equals(user1) 43.session.removeAttribute(name);44.res.sendRedirect(req.getContextPath() + LOGIN_PAGE);45.return;9.chain.doFilter(re

15、quest, response);50.51.52.public void init(FilterConfig arg0)53.throws ServletException54.55.56.創(chuàng)建用于攔截驗(yàn)證身份的 AdminAuthenticationFilter.javahtml view plain copy1.package com.axb.cheney.filter;.import java.io.IOException;6.7.import javax.servlet.Filter;8.import javax.servlet.FilterChain;9.import

16、 javax.servlet.FilterConfig;10.import javax.servlet.ServletException;11.import javax.servlet.ServletRequest;12.import javax.servlet.ServletResponse;13.import javax.servlet.http.HttpServletRequest;14.import javax.servlet.http.HttpServletResponse;15.import javax.servlet.http.HttpSession;16.17.public c

17、lass AdminAuthenticationFilter18.implements Filter19.20.private static String LOGIN_PAGE = /login.jsp;21.22.public void destroy()6.public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)27.throws IOException, ServletException28.29.HttpServletRequest req =

18、(HttpServletRequest)request;30.HttpServletResponse res = (HttpServletResponse)response;31.32.String currentUrl = req.getServletPath();33.34.HttpSession session = req.getSession();35.36.System.out.println(AdminAuthenticationFilter);37.if (currentUrl.equals() currentUrl = currentUrl + /;38.if (current

19、Url.startsWith(/) & (!currentUrl.startsWith(/login.jsp) 39.String user = (String)session.getAttribute(name);40.if (user = null) 41.res.sendRedirect(req.getContextPath() + LOGIN_PAGE);42.return;43.44.if (!user.equals(admin) 45.session.removeAttribute(name);46.res.sendRedirect(req.getContextPath() + LOGIN_PAGE);47.return;48.49.50.chain.doFilter(request, response);51.52.53.public void init(FilterConfig arg0)54.throws ServletException55.56.57.最后配置 web.xml 文件用于過濾 admin 和 user 目錄下的資源訪問html view plain copy.SAML7.8.9.i

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論