|
@@ -2,6 +2,7 @@ package com.yunzhi.nanyang.shiro.filters;
|
2
|
2
|
|
3
|
3
|
import com.yunzhi.nanyang.shiro.utils.JWTToken;
|
4
|
4
|
import com.yunzhi.nanyang.shiro.utils.JWTUtil;
|
|
5
|
+import org.apache.shiro.authc.AuthenticationException;
|
5
|
6
|
import org.apache.shiro.authc.AuthenticationToken;
|
6
|
7
|
import org.apache.shiro.subject.Subject;
|
7
|
8
|
import org.apache.shiro.web.filter.authc.AuthenticatingFilter;
|
|
@@ -13,6 +14,8 @@ import javax.servlet.ServletRequest;
|
13
|
14
|
import javax.servlet.ServletResponse;
|
14
|
15
|
import javax.servlet.http.HttpServletResponse;
|
15
|
16
|
import java.io.IOException;
|
|
17
|
+import java.io.UnsupportedEncodingException;
|
|
18
|
+import java.net.URLEncoder;
|
16
|
19
|
|
17
|
20
|
public class JWTFilter extends AuthenticatingFilter {
|
18
|
21
|
|
|
@@ -38,13 +41,21 @@ public class JWTFilter extends AuthenticatingFilter {
|
38
|
41
|
return new JWTToken(authorization);
|
39
|
42
|
}
|
40
|
43
|
|
|
44
|
+ @Override
|
|
45
|
+ protected boolean executeLogin(ServletRequest request, ServletResponse response) throws Exception {
|
|
46
|
+ AuthenticationToken token = this.createToken(request, response);
|
|
47
|
+ Subject subject = this.getSubject(request, response);
|
|
48
|
+ subject.login(token);
|
|
49
|
+ return this.onLoginSuccess(token, subject, request, response);
|
|
50
|
+ }
|
|
51
|
+
|
41
|
52
|
@Override
|
42
|
53
|
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
|
43
|
54
|
boolean allowed = false;
|
44
|
55
|
try {
|
45
|
56
|
allowed = executeLogin(request, response);
|
46
|
57
|
} catch (Exception e) {
|
47
|
|
- response401(request, response);
|
|
58
|
+ response401(request, response, e);
|
48
|
59
|
}
|
49
|
60
|
|
50
|
61
|
return allowed;
|
|
@@ -73,11 +84,20 @@ public class JWTFilter extends AuthenticatingFilter {
|
73
|
84
|
/**
|
74
|
85
|
*Jump illegal request to / 401
|
75
|
86
|
*/
|
76
|
|
- private void response401(ServletRequest request, ServletResponse response) {
|
|
87
|
+ private void response401(ServletRequest request, ServletResponse response, Exception e) {
|
|
88
|
+ String message = "";
|
|
89
|
+ if (e != null) {
|
|
90
|
+ try {
|
|
91
|
+ message = URLEncoder.encode(e.getMessage(), "UTF-8");
|
|
92
|
+ } catch (UnsupportedEncodingException ex) {
|
|
93
|
+ //
|
|
94
|
+ }
|
|
95
|
+ }
|
|
96
|
+
|
77
|
97
|
try {
|
78
|
|
- WebUtils.toHttp(response).sendRedirect(unauthorizedUrl);
|
79
|
|
- } catch (IOException e) {
|
80
|
|
- LOGGER.error(e.getMessage());
|
|
98
|
+ WebUtils.toHttp(response).sendRedirect(unauthorizedUrl + "?msg=" + message);
|
|
99
|
+ } catch (IOException e1) {
|
|
100
|
+ LOGGER.error(e1.getMessage());
|
81
|
101
|
}
|
82
|
102
|
}
|
83
|
103
|
}
|