TA的每日心情 | 衰 2021-2-2 11:21 |
|---|
签到天数: 36 天 [LV.5]常住居民I
|
Sturts2漏洞百出,可谓是年年都会爆发一次或大或小的风波,但是即使这样,Sturts2还是有一批用户的。
& f2 S) f) ]% P" r花费十几分钟,简单的搭建实现了一下基本的登录功能。都是入门级的,高手肯定是不屑了,但是对于初次涉猎的小白们,还是可以提供点参考的。
& }2 L2 y; r8 u/ I) C& t5 U
9 M3 r! b3 v. I% D5 G" y注意:/ v" a0 ?. v! w! P
0)项目Demo可以正常运行并经过测试3 l8 k) B1 P: n; m; c( p' t$ c( w
1)代码是伪代码,自行添加逻辑7 ?3 J2 U4 o0 k {$ z/ b
2)struts2-2.5.10 加入了Action验证,注意 struts.xml 中的 <global-allowed-methods>regex:.*</global-allowed-methods># y3 l9 ]0 G' ?+ \9 ~# r8 m
3)struts2-2.5.10 拦截器修改了路径,注意web.xml中的配置
- w( [! B% r) U/ I4 L. N6 `) y4)本案例,返回采用json数据的形式,使用struts2-json-plugin-2.3.20.jar,json配置见struts.xml 全局配置。
% |9 {& L: |1 C' C) Y* \4 L
7 H% [( O2 |0 Q1 b; f一、项目搭建(使用最新版本struts2-core-2.5.10)
. U& f# B. W+ n2 ?( \# O; F# M; e" O e' x7 I
1、相关JAR
8 l# w3 M9 r4 n0 Y7 C9 h- mcommons-fileupload-1.3.2.jar
& r. x8 j1 ~( v9 q3 B3 Y7 Xcommons-io-2.4.jar
( g* |# h5 _( D: X% U3 kcommons-lang3-3.4.jar# ?$ `: i, `& V" @. d; F8 h; n
commons-logging-1.2.jar) P/ l. o+ l& S9 c: X" g6 i
freemarker-2.3.23.jar
5 T6 R9 w( n; j& F Rjavassist-3.20.0-GA.jar
1 A7 @ Z" f( [ l8 Z Wlog4j-api-2.7.jar
$ u6 D1 i5 @: V; N: flog4j-core-2.7.jar
" d( @' C7 Z) D" h& z* Aognl-3.1.12.jar% O1 Z/ P5 d8 E- Y3 f7 U0 ~2 C
struts2-core-2.5.10.jar
e% [% E' z4 ?& Z0 d3 e- x% e- ?struts2-json-plugin-2.3.20.jar
5 \4 G" a2 H3 u* G+ m9 C8 Kxwork-core-2.3.31.jar
/ A& ^0 }/ H" x/ S [4 I
3 o' C7 o9 p* g6 Q: }2、前端框架1 V: X. o1 f9 I$ b% [" a
Jqueryjquery-1.10.2.min.js8 j* e5 |9 [) m$ x @# v
bootstrap.min.js
4 l( ^: U4 [" B; t. J4 P5 K# K3 C- B) x$ a
3、相关配置:
8 k& h" J( V- W& I. B# H2 qweb.xml
6 A; ~ y; [/ n ]) |% G$ ~% i- <?xml version="1.0" encoding="UTF-8"?>; R) H/ q3 D4 j$ ^$ [/ ^
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5" >) S) Y7 M7 V" P2 \7 f
- <display-name>struts2</display-name>
$ v' Y* r H, C& I - <!-- struts2配置 -->/ _9 p. v3 N" l8 |3 H; ?
- <filter>
$ h8 X& K5 l; |1 I/ G# t - <filter-name>struts2</filter-name>, L2 k& U0 S7 \8 v; s' @. V, l0 c
- <filter-class>
, r9 A) V7 w- k: G - org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter5 }# x2 J1 O: M' h+ }8 G
- </filter-class>
& `6 z5 v# x/ z' v& m x& K9 L - </filter>
/ x- O# |3 s3 f( H - <filter-mapping>
( d! d% o" q1 {8 ] - <filter-name>struts2</filter-name>
' b8 t" d' D+ g* @& f2 A" u8 R - <url-pattern>/*</url-pattern>, u6 N9 {) j% K! _1 O( l
- </filter-mapping>- p1 j& ?3 |: A
- <welcome-file-list>7 x; R+ p$ k G7 F+ ^0 O0 x
- <welcome-file>login.jsp</welcome-file>
3 V9 u- d) J ~: q - </welcome-file-list>
" X) c( Z" |! M - </web-a
复制代码 ' @. i8 U9 R& X+ z- z" u
struts.xml$ ~+ j. Q0 [7 u# o9 \6 v1 X# \! s
- <?xml version="1.0" encoding="UTF-8" ?>$ a7 W1 F; r0 U" z% \. X
- <!DOCTYPE struts PUBLIC
* k) ]7 o- m \' W- t& R - "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"! q; ]2 `# _. Q
- "http://struts.apache.org/dtds/struts-2.5.dtd">2 M& S+ J: X0 v# J3 S% J5 S
- <struts>% u8 [: H3 b- G. n! G
- <!-- struts配置文件改动后,是否重新加载 -->
! X* k0 J! W, O& C: J9 A# b - <constant name="struts.configuration.xml.reload" value="true" />( T2 N, r1 t }; S2 C) S, Z
- <!-- 开发模式 开启它则默认开启了i18n.reload、configuration.xml.reload 上线后要关闭此选项 -->
. G( Q3 V# D" b. `( @& |# g - <constant name="struts.devMode" value="false" />& D) o: Q5 s A+ f
- <constant name="struts.i18n.encoding" value="UTF-8"/>
2 K% x3 s4 I. H1 M# t! A - <constant name="struts.multipart.maxSize" value="1073741824"/><!-- 上传文件最大值 -->: s- ^& n$ l3 T; b) d$ D
- <constant name="struts.custom.i18n.resources" value="config" />$ S6 T; V2 N! h6 H& N; [4 g; h" m' S
- <constant name="struts.action.extension" value="action"/>
2 n ?! L! {5 v4 J6 ]/ ^ - <package name="struts-global" extends="json-default">1 d# b2 l7 N2 \8 {5 p9 ?* w. y5 l: ?
- <!-- 配置Json输出 配合使用 struts2-json-plugin-2.3.20.jar 后台配置 message -->
: M4 Z. |' d8 t) P5 R+ b6 Z0 M - <global-results> 2 |$ E6 P4 U) W7 i
- <result type="json">3 O- x' d0 \" z& E) }& b+ a% B2 R
- <param name="root">message</param>
: A6 H7 V1 P0 S3 b7 ^1 `2 o - </result>
6 J/ U! p; R* D9 n" p, j; L - </global-results>
7 _3 [' z$ I$ a - <!-- 2.5.10版本 匹配 -->
/ T+ G6 `/ b/ E4 b5 F - <global-allowed-methods>regex:.*</global-allowed-methods>/ c7 H3 k5 v) w/ S
- </package>
3 g+ n2 f: S$ o% t; G/ m, S - <package name="user" extends="struts-global">9 c( N; {# n2 n5 ^
- <action name="userAction_*" class="com.itstyle.web.user.UserAction" method="{1}">% v( o) c% M# z7 X
- <result name="logout">/login.jsp</result>& X& J$ f5 w2 F3 f4 P2 [9 m5 o- d
- </action>
$ b$ _, S* e8 ~1 h" S; x, y. E - </package>
4 K. `; F4 }" ^ - </struts>
6 t% r k* Y' ^( n( ^9 g
5 R, d u. Z' c
复制代码
J3 @- s% k' ]% E& {, _. |4 b* C5 U' {" L! J, F
UserAction, H% A( y8 W7 j7 |+ R
- package com.itstyle.web.user;
' f$ u$ o7 \ o5 I9 V
, e: h( u" @1 [/ B/ B7 e) ?- import com.opensymphony.xwork2.ActionSupport;
6 W6 H0 u$ c* Q- \1 c0 Q - /**
- B% C/ s/ e u% D+ _ - * ' r/ p8 ?: R! t+ m; H- }
- * @author 科帮网2 J. |" B$ z, x' u/ v$ D
- *5 L& |0 [, j# O! _7 V
- */
$ D1 O* _ F0 o n - public class UserAction extends ActionSupport {$ e) S' V2 C2 z
- private static final long serialVersionUID = 1L;8 t9 ~1 C: K; D: S
- private String message;1 _3 t- }% B6 c6 t6 q
- private String userName;
, o4 ~. c1 B& {. M: k1 n - private String password;# J# _4 O$ I) p2 ^7 @$ C! P
- 1 g# V) f5 Y, f- `: Q' H' Y
- public String login(){# q, \# Z% x1 i. c+ N8 B
- if("admin".equals(userName)&&"admin".equals(password)){5 i* e8 s+ s* }
- message = "0";) i$ `5 V0 } G2 Z
- }else{
. t; D, k# q5 p5 j - message = "1";
4 Y& U, n7 d, w# z( ` - }
# W$ R* u2 }$ \) E P' \ - return SUCCESS;
* n a! g) R$ f1 N, O7 v# } - }& C4 h- e5 l9 l' ^6 {2 _; q
- H& m: N) V& \
- public void setMessage(String message) {+ f* a8 m5 c3 v6 T
- this.message = message;
8 s9 u9 n% M) [. W- M- G' W - }
+ N" d* C& X: B' n- T6 `9 v( ] - public String getMessage() {
. R) F$ D) X# O- r. t1 x+ z - return message;
+ H8 E. }$ j9 v; _ - }7 d, x7 e i4 U
3 h( x% q# R; m6 w |3 z' K5 G- public void setUserName(String userName) {
4 p3 f0 s0 J/ H; m8 ^5 ~ - this.userName = userName;5 L& \# p+ i! ]+ F1 O( l" d
- }' g4 \3 W \ \. B& S& N& N$ C3 t
, \8 `6 r3 @- F6 k5 T- public void setPassword(String password) {
1 o+ F3 U6 r: ]0 G" `- \* T - this.password = password;5 x! @. h4 E. `; U$ g4 H0 y
- }
0 } W; e4 {! p( M5 B. [ - }
; D) u- v- V* r# a7 E
复制代码
7 m) X' f/ [: T5 V2 o" [% R q/ F" E$ X# l
login.jsp
) l+ E3 C, @# ^+ V' Q- <script>3 Y4 \! T6 `2 Y
- var path = '<%=basePath %>';* N% _( _) B6 Q; d( D5 q) S
- function login(){
% K- ]6 R J# p+ q9 ` - var username = $("#username").val();$ h! j9 ]) s: H( W, s( ^. i
- var password = $("#password").val();2 D0 a; i) S" p/ e. ?; f
- var data = {username:username,password:password};
) Y) {5 S5 y. }2 l$ e - $.ajax({/ ~8 [' c$ \9 ~' T8 O: {
- type:"post",
% `; O* I9 ?7 m- W+ {# }; w5 K& Z8 ] - url:path+"userAction_login.action", y3 B3 v) I& [' ~9 q
- data:data,
, n i6 H' o8 I3 |9 P: Q4 f - dataType:"json",
3 ]0 _; a& A/ b' A: J8 i - async : false,
7 c+ A4 w8 ]) o2 _ - success : function(data) {; @, x& k0 M) w9 D- j6 q/ O
- if(data==="0"){; s9 D/ Z! k1 J6 ?% q( n9 U
- alert("登录成功");
/ {# F4 W0 d, |' a7 k - window.location.href ="http://blog.52itstyle.com";# m4 o: a; s7 r3 J$ {& Z. P6 q
- }else{5 b" m' [( j; k) K1 G
- alert("登录失败");
: n. y9 f1 [) _/ S - }; R1 k& X# S: z- x6 |. b
- }: \6 V/ t1 I! O& P+ O
- });
2 m8 J+ P8 G9 J* K; I& r9 ] -
$ ]. D4 {+ C0 z - }
0 h- J3 v2 `, z6 M: z8 Q - d' j" a9 x) @$ ]
- </script>
复制代码
: Z2 d4 r5 h* U5 j5 e& e. L页面展示:. J& O, s3 q; R9 e
! a! X' s4 j: W9 L; B
; o5 ~8 ~0 O4 P" E! m7 |% u
, ~' I/ M, J' r* M' j
基于Sturts2实现一个简单的登录功能.txt
(64 Bytes, 下载次数: 2, 售价: 1 IT币)
' ~) T5 p# [9 }! P# v' C1 \
|
|