TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
Sturts2漏洞百出,可谓是年年都会爆发一次或大或小的风波,但是即使这样,Sturts2还是有一批用户的。
* ~5 A+ @4 `( X+ D花费十几分钟,简单的搭建实现了一下基本的登录功能。都是入门级的,高手肯定是不屑了,但是对于初次涉猎的小白们,还是可以提供点参考的。
; i/ D4 H* f) W( K
4 ~1 v( |( A( M) ~, \3 ], q注意:
3 f% n2 r6 N5 D0)项目Demo可以正常运行并经过测试% r0 {5 e5 q, ^' \
1)代码是伪代码,自行添加逻辑3 P# z5 f) v6 L6 Y
2)struts2-2.5.10 加入了Action验证,注意 struts.xml 中的 <global-allowed-methods>regex:.*</global-allowed-methods>! o% ?& Y4 D8 }9 J6 X/ B0 p; p
3)struts2-2.5.10 拦截器修改了路径,注意web.xml中的配置
/ I/ E' W- e/ r9 F1 Y6 {4)本案例,返回采用json数据的形式,使用struts2-json-plugin-2.3.20.jar,json配置见struts.xml 全局配置。% ?( w/ w {. q
6 k9 n7 v* b( k! e一、项目搭建(使用最新版本struts2-core-2.5.10)3 J9 J& d, o& i
* v' n/ R, ^' I4 Y0 B' D2 c& v$ L
1、相关JAR8 P0 F% J; I+ P8 {( q
commons-fileupload-1.3.2.jar
7 P. m) K' }! H0 L* t# \5 r3 ycommons-io-2.4.jar* \% f$ A2 y" }: X3 W+ B! H
commons-lang3-3.4.jar
- o* E- h; U: {commons-logging-1.2.jar0 r/ l5 O0 |3 o+ }) _
freemarker-2.3.23.jar% h% |. p6 H: C8 p+ E3 J
javassist-3.20.0-GA.jar. w$ Z* n- K: V# b! h
log4j-api-2.7.jar
) _* O- \* f$ l7 V8 ~) D4 {& a' Slog4j-core-2.7.jar
7 e0 i3 l: t1 V6 ~: _! ~ognl-3.1.12.jar
% w5 r# T5 m9 L h3 M6 Ystruts2-core-2.5.10.jar2 Y6 P% h5 O# l' }& ~) f1 u: f
struts2-json-plugin-2.3.20.jar
" p4 M8 o% p# D, Jxwork-core-2.3.31.jar: m( M( |; D# d1 C. \4 l: m5 k
2 F+ K( y5 m. s; @, O/ R/ H2 ?+ @
2、前端框架( B- T t5 L& n p$ D
Jqueryjquery-1.10.2.min.js+ k1 ]$ I I. n7 d) ^
bootstrap.min.js! _5 i6 T" q; \
& ~6 N& @: w! v" H3 n1 X3、相关配置:
/ H# G( p) J0 `; Z3 _2 r5 Zweb.xml4 L$ v" v. D: H7 V) |/ Q
- <?xml version="1.0" encoding="UTF-8"?>" K( V' G) f: Z8 }: S9 D4 p
- <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" >8 q. ?0 [* Z+ t5 \7 r; L- g
- <display-name>struts2</display-name>; F7 C G$ G: f0 N- t4 q( z* H
- <!-- struts2配置 -->
$ p) z8 t: [+ w# l# X - <filter>
% h" D) n* t) V8 _$ D$ z: B - <filter-name>struts2</filter-name>3 q; S3 `* t4 {3 ]8 {/ T T
- <filter-class>! m6 W1 J9 Y3 n. n2 M5 w H
- org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter8 ^8 q, {! g% K) q- Y. s. K5 |
- </filter-class>
) m, k& F1 @3 n. ~) | - </filter>
9 ]& D4 ]. R$ A- n; J5 |, O - <filter-mapping>4 T4 p" r3 z! m& [- s
- <filter-name>struts2</filter-name>; o1 C/ n9 s# F8 ~
- <url-pattern>/*</url-pattern>
, l) ?& a8 w; I" e" x - </filter-mapping>
9 N2 H; F+ l5 G( w: M. _ - <welcome-file-list>
; x- R) i2 I2 y% w# J) [ - <welcome-file>login.jsp</welcome-file>! Y; F" o6 N: t J7 U4 V3 o
- </welcome-file-list>* F' g8 i V# c# J& E2 P1 F1 w' _
- </web-a
复制代码 9 W e& U; R5 z3 I9 V
struts.xml
0 R! f0 S9 }) Q: @- <?xml version="1.0" encoding="UTF-8" ?>
2 _- t. _" ? l$ y, S - <!DOCTYPE struts PUBLIC
4 d& f3 J. t2 f. {, k' N$ j+ |- j - "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
B" b$ z6 ]' r( L Z - "http://struts.apache.org/dtds/struts-2.5.dtd">
$ c o$ M* n; f5 v0 g9 z - <struts>8 M& X5 U; l( ?, r; p5 g, l
- <!-- struts配置文件改动后,是否重新加载 -->
- N/ m1 p2 p* x$ H - <constant name="struts.configuration.xml.reload" value="true" />+ j9 |, b# m, k7 S2 X, O3 ?
- <!-- 开发模式 开启它则默认开启了i18n.reload、configuration.xml.reload 上线后要关闭此选项 -->
0 ~( z6 I. z) H5 h* a - <constant name="struts.devMode" value="false" />
( g" e+ ] e0 W+ r: j5 \ - <constant name="struts.i18n.encoding" value="UTF-8"/>
9 h' R8 q8 r# N( e* @# @ - <constant name="struts.multipart.maxSize" value="1073741824"/><!-- 上传文件最大值 -->
0 Z/ W. \$ h. L- L X - <constant name="struts.custom.i18n.resources" value="config" />$ \8 n8 n0 I# [+ w) w2 H* M9 [
- <constant name="struts.action.extension" value="action"/>
5 M+ i# w, ]# r* I - <package name="struts-global" extends="json-default">; S6 l0 f/ l! j8 L" [6 q2 X1 I0 X
- <!-- 配置Json输出 配合使用 struts2-json-plugin-2.3.20.jar 后台配置 message -->: n" y$ d+ @: J: f6 [* p# I$ T& k1 |
- <global-results> + m! p/ j5 U4 ~* p# ~$ k7 d
- <result type="json">2 O! Y. M9 V/ r: |) X/ r* J3 m0 w
- <param name="root">message</param># h: R, B! h' A; o( A) w% g7 g
- </result>
; [3 @. r( P. `% z+ a# ?: _ - </global-results>
y& @, l' F6 x# m: V5 N6 o - <!-- 2.5.10版本 匹配 -->& s" X% E( u$ c( p2 T& d( V0 H
- <global-allowed-methods>regex:.*</global-allowed-methods>7 }# U) w& \* `: x
- </package>2 _( N4 S/ p% ~ W1 p" T
- <package name="user" extends="struts-global">
4 {$ v0 I7 l0 y: R9 F T. i! ^7 V - <action name="userAction_*" class="com.itstyle.web.user.UserAction" method="{1}">
( M8 x; q4 y* E- s; i% O/ \% \ - <result name="logout">/login.jsp</result>, h6 f: S* r# c! ?9 [
- </action>! R. B5 ]5 V0 ^7 Q
- </package>
* ]+ V# I" }6 v6 A - </struts>
5 ^7 L3 E4 j" W' u$ k- | - ( |) n: c; i0 u0 J; L! P! ? [! k
复制代码 ) g! @$ X0 k- L Y( k; @' Z2 |
' {- @- \8 X1 k' @6 AUserAction
* E* Y5 `9 i* A2 C' M( [; @4 n- package com.itstyle.web.user;8 H( D* {, F0 R& `- _
& w/ z/ ]# h, w3 O3 c- import com.opensymphony.xwork2.ActionSupport;; R( E) a4 ?9 H8 N
- /**. P( B1 F$ |: {" A6 g
- *
! e4 T ]# E/ l/ f! E7 ~" ~ - * @author 科帮网
1 c8 u! h$ ~0 i! j0 c - *9 a U' H. W0 M& M. f% p: f
- */, G! Z4 e- V8 T/ E- b
- public class UserAction extends ActionSupport {4 ` u/ x8 l! z2 g& c/ j) y
- private static final long serialVersionUID = 1L;
0 e/ u' p* z& g) u - private String message;5 R2 {+ P" B4 D6 r( O) U
- private String userName;
. s! z$ ^* d0 s$ X - private String password;, y/ D0 z; R4 e3 V) u
-
9 B$ _& y/ M4 [% A% E - public String login(){: e! F' Y/ l( k1 |7 }- y3 x
- if("admin".equals(userName)&&"admin".equals(password)){' ]$ P+ d4 q- w
- message = "0";' x( F) t6 x4 D0 U, R! S
- }else{
9 ?4 P% Q. ~+ ] b$ L - message = "1";; d. C$ S( M0 `
- }
/ h3 m2 G6 y; M - return SUCCESS;2 t9 b( G$ m( {9 i: o
- }; v- K4 h# g( O# u6 Z" i
- ( K9 M( N/ c& n& ?# m X
- public void setMessage(String message) {, h; N9 h( f* `! L/ P4 \' v1 X$ h
- this.message = message;( B! V# y( Z& V$ m$ m& O( T8 O
- }+ ^/ i. w8 K9 H q; C
- public String getMessage() {
) R) ~4 D! q1 Z/ x" { - return message;
: k4 N/ x- A z' b0 _% }/ x2 N# d - }
' k& ^7 [3 p# X - " ?) y( n/ j c8 X
- public void setUserName(String userName) {/ d& e2 b6 w! K8 I; y
- this.userName = userName;" }2 G# L# H6 p2 z# Q3 J
- }
; l m- a0 L' C2 {; @7 V3 B
6 N& e8 ?0 H3 S( Z7 ~8 d- public void setPassword(String password) {/ \& j7 B' |8 m9 ]4 l
- this.password = password;
% T* g7 p4 a/ ~' m* Y' `. D% Z - }7 {' k6 i4 b7 ?) Y
- }) b* v6 t) L& A R
复制代码
w- A$ Z( J2 b% t( C" j6 K0 m7 k
2 z0 n* y9 A) Z; `# a' o- Elogin.jsp
7 J, Q* J, [3 q- <script>
6 u/ u \* B& \6 s( G - var path = '<%=basePath %>';
' {# W% D2 I) s - function login(){" X, t) B+ C& J
- var username = $("#username").val();/ [2 e1 l' o: C) T
- var password = $("#password").val();
( S d8 u+ x l2 |! K - var data = {username:username,password:password};- \1 e% D" c! P6 S2 F) x9 R
- $.ajax({
- }# M3 q# R9 L* h1 G/ K - type:"post",
4 N7 I/ g& n! N1 m - url:path+"userAction_login.action",
3 i" g$ } o2 V* z: |6 I; w - data:data,
4 R. t1 C. m* p4 L* o+ x7 j - dataType:"json",
( `) ^3 B! b: {& D/ `. p2 g) f' v - async : false, o! c) }, `# S1 W4 z* {
- success : function(data) { S- R: O* J# K/ y
- if(data==="0"){
8 b# x( L, O9 x( ]( E! s - alert("登录成功");
! K# v3 v0 R8 d5 t - window.location.href ="http://blog.52itstyle.com";
+ R! H0 G$ \. n. A0 I - }else{
+ f+ q9 D1 F ]- N - alert("登录失败");
# s% g6 k. c, Q7 |! b% D/ n - }
' W' y: m9 ^( P1 T9 v T - }
5 i: m J) B$ w' K0 W2 V - });
3 v X( Q6 `! `7 f -
% P$ y+ c! j( Z# N) q - }# r4 U! e$ z0 [! h% t7 u
- - x6 u! f" ?; Z$ a; `, J
- </script>
复制代码 0 \$ j' h/ G* ^( \; L! d
页面展示:
) _$ }& X* ]1 o$ o4 v+ D5 Z- l2 B9 M I- {5 n
: E, P; X) n% X( Q' ], f4 x
; g. I; \1 B$ L6 P% i
基于Sturts2实现一个简单的登录功能.txt
(64 Bytes, 下载次数: 2, 售价: 1 IT币)
$ o, g$ f( h% U% V7 _ |
|