% r. C, F: n' [- [5 e; ^) @1 r2 lif ( !currentUser.isAuthenticated() ) { ( O9 V( Z5 w$ k7 y2 h9 M $ I0 p v" _) J# m+ ?* C5 {//collect user principals and credentials in a gui specific manner + p* n7 P) u d5 b; u. X7 y, \) V: s m' H3 V& {# w( `* a
//such as username/password html form, X509 certificate, OpenID, etc. + g1 a1 x( ]; b+ |7 n0 ~$ M, @% T8 K9 t* Q- ~
//We'll use the username/password example here since it is the most common., ~0 l9 n; }" G2 p/ ~
) H# i9 j4 w9 V' h+ w' X//(do you know what movie this is from? ;) % B( @2 B0 R6 h/ g : V5 i6 O$ s& `UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");+ o2 D1 ^1 o: |( q/ p, ?- j( j1 a
* n1 ?( B! G+ |! b W1 _4 X2 A
//this is all you have to do to support 'remember me' (no config - built in!): + m! {5 M+ b& O- c$ x 7 _% B8 e! B% `+ Q ^ \. \9 V2 ltoken.setRememberMe(true);' D* _, ?$ c6 T- ^4 V) w
7 n: R+ b4 {' S. K: b% P5 t
currentUser.login(token);7 F1 |7 @# k" D! y$ k( x) W0 N; n
. V- F% ^! H J5 E}; L1 J$ c+ {( O7 S/ A, L6 k: q4 Z
2 h) x) B* e/ r$ t3 G就是这样,太简单了吧!7 p: O) A: {$ `9 F
E ~& K, G8 t" R1 z3 Z
那登录失败了怎么处理呢?可以通过捕获各类异常,根据不同类型的异常做出不同的处理:' D! C, H0 m' ~6 H* R, F
2 }$ G9 a, u/ V. b. D J9 _3 Q
try { 6 p( l5 R/ T- _. p. R5 E: X: Z 7 l- H3 k6 a9 g' D9 c6 k4 ]# C1 McurrentUser.login( token );) Z8 f& j$ w8 o) n0 p- a
7 z) V" Y: d$ D7 _
//if no exception, that's it, we're done!! H* P+ y: l: Q$ g0 v
; Y8 m8 n; i5 B1 ~5 m} catch ( UnknownAccountException uae ) { ! n5 M; ]' {+ r' [9 X ! ?8 J" L" C! r' D3 {+ k//username wasn't in the system, show them an error message?6 i4 t: j% V. A& G$ Y+ Y# e# W# m
5 X- a4 Q: |/ T; K. y& i} catch ( IncorrectCredentialsException ice ) { 6 z o% x, I5 S0 Z9 g' k5 i& K* l. u* m6 D; g
//password didn't match, try again?# w+ r R$ O- E$ e0 P W" ~
) F, ?. z2 b7 F R6 g
} catch ( LockedAccountException lae ) { & \! v3 Y+ D# M" n( T0 D2 u8 x . j* g/ a4 g9 b' N7 F; U//account for that username is locked - can't login. Show them a message? ) H2 g9 z1 c2 |; A& u! y0 {3 X G% {6 {! H) ?/ ?. x
}) A: ?7 z- g# p* ^4 R7 a0 {/ W
9 p. ^. Q! X4 {8 b! ?4 r... more types exceptions to check if you want ... 3 z5 b6 V" Z: u2 g! T; i; q6 m# ^# r, Z% z1 `2 k# g6 M
} catch ( AuthenticationException ae ) { 5 K2 D' U/ ^% v: ?- m5 r3 s6 M4 O& S+ ^! H* C
//unexpected condition - error? W$ T7 b. `* g( Y 8 ^1 d$ w% L; W3 n, x2 o}4 ?+ z N5 c. e* z+ }( {8 f9 l
* H" e' w8 t1 o3 B可以捕获Shiro提供的各种异常,也可以抛出自定义类异常用于处理Shiro未考虑到的情况。预知详情,可以去了解AuthenticationException JavaDoc。 # v/ X( n% h2 h, I. v8 p! Z0 W. F& o
提示:最安全的做法是将登录失败的消息告知用户,你总不会帮助攻击者入侵你的系统吧!, \& \' ]& D- x4 _- c8 o
1 r) v- a& y# ^ X6 W2 x! J! POK,现在已经拥有一个登录用户了,我们还能做点儿什么呢? 4 |/ Y% f. ]7 b9 g- P( J" L7 e' k p5 E5 n5 A
比方说,他们是谁:; E7 Y: [8 |% {
& n! t9 F, r' \( f M4 F//print their identifying principal (in this case, a username): 1 F0 |$ e% G: o) {$ Q 8 P6 G3 h ^, s2 \log.info( "User [" + currentUser.getPrincipal() + "] logged in successfully." );2 c3 m; W. w) \/ O+ X8 V/ S: G
; q4 g/ m$ F3 R& c也可以判断用户是否拥有特定的角色: ! u* N" J- n5 X% g$ d5 _; t- Z9 T( x
if ( currentUser.hasRole( "schwartz" ) ) { / U, y2 ]+ G7 `8 T3 ~% C. d' F. Q" a$ s+ u
log.info("May the Schwartz be with you!" ); % n( ]& ?6 F/ \/ x# r' B; [# B8 e& ^8 H0 B, {; _' w
} else {% Z& \8 p7 S0 c8 G2 R2 Y
. F0 b! V6 e# `+ elog.info( "Hello, mere mortal." ); * `* ^0 y! G0 z5 ]5 e# n7 e( h* j& d& X: K) T. ?6 O
} ! p9 n3 C7 j) _* ~( }$ v7 _! C0 Q' b# l6 T% z1 O Z$ j( X- c# T
还可以判断用户是否对特定某实体有操作权限:! B+ Y7 k" s8 G2 v7 M