|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.acegisecurity.adapters.jboss; |
|
17 |
| |
|
18 |
| import org.acegisecurity.Authentication; |
|
19 |
| import org.acegisecurity.context.SecurityContextHolder; |
|
20 |
| |
|
21 |
| import org.apache.commons.logging.Log; |
|
22 |
| import org.apache.commons.logging.LogFactory; |
|
23 |
| |
|
24 |
| import java.io.IOException; |
|
25 |
| |
|
26 |
| import java.security.Principal; |
|
27 |
| |
|
28 |
| import java.util.Iterator; |
|
29 |
| |
|
30 |
| import javax.naming.Context; |
|
31 |
| import javax.naming.InitialContext; |
|
32 |
| import javax.naming.NamingException; |
|
33 |
| |
|
34 |
| import javax.security.auth.Subject; |
|
35 |
| |
|
36 |
| import javax.servlet.Filter; |
|
37 |
| import javax.servlet.FilterChain; |
|
38 |
| import javax.servlet.FilterConfig; |
|
39 |
| import javax.servlet.ServletException; |
|
40 |
| import javax.servlet.ServletRequest; |
|
41 |
| import javax.servlet.ServletResponse; |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| public class JbossIntegrationFilter implements Filter { |
|
61 |
| |
|
62 |
| |
|
63 |
| private static final Log logger = LogFactory.getLog(JbossIntegrationFilter.class); |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
0
| public void destroy() {}
|
|
71 |
| |
|
72 |
6
| public void doFilter(ServletRequest request, ServletResponse response,
|
|
73 |
| FilterChain chain) throws IOException, ServletException { |
|
74 |
6
| Object principal = extractFromContainer(request);
|
|
75 |
| |
|
76 |
6
| if ((principal != null) && principal instanceof Authentication) {
|
|
77 |
1
| SecurityContextHolder.getContext().setAuthentication((Authentication) principal);
|
|
78 |
| |
|
79 |
1
| if (logger.isDebugEnabled()) {
|
|
80 |
0
| logger.debug(
|
|
81 |
| "ContextHolder updated with Authentication from container: '" |
|
82 |
| + principal + "'"); |
|
83 |
| } |
|
84 |
| } else { |
|
85 |
5
| if (logger.isDebugEnabled()) {
|
|
86 |
0
| logger.debug(
|
|
87 |
| "ContextHolder not set with new Authentication as Principal was: '" |
|
88 |
| + principal + "'"); |
|
89 |
| } |
|
90 |
| } |
|
91 |
| |
|
92 |
6
| chain.doFilter(request, response);
|
|
93 |
| } |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
0
| public void init(FilterConfig arg0) throws ServletException {}
|
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
1
| protected Context getLookupContext() throws NamingException {
|
|
112 |
1
| return new InitialContext();
|
|
113 |
| } |
|
114 |
| |
|
115 |
6
| private Object extractFromContainer(ServletRequest request) {
|
|
116 |
6
| Subject subject = null;
|
|
117 |
| |
|
118 |
6
| try {
|
|
119 |
6
| Context lc = this.getLookupContext();
|
|
120 |
| |
|
121 |
6
| if (lc == null) {
|
|
122 |
1
| if (logger.isWarnEnabled()) {
|
|
123 |
1
| logger.warn("Could not obtain a Context to perform lookup");
|
|
124 |
| } |
|
125 |
| |
|
126 |
1
| return null;
|
|
127 |
| } |
|
128 |
| |
|
129 |
5
| Object result = lc.lookup("java:comp/env/security/subject");
|
|
130 |
| |
|
131 |
5
| if (result instanceof Subject) {
|
|
132 |
3
| subject = (Subject) result;
|
|
133 |
| } |
|
134 |
| } catch (NamingException ne) { |
|
135 |
0
| if (logger.isWarnEnabled()) {
|
|
136 |
0
| logger.warn("Lookup on Subject failed "
|
|
137 |
| + ne.getLocalizedMessage()); |
|
138 |
| } |
|
139 |
| } |
|
140 |
| |
|
141 |
5
| if ((subject != null) && (subject.getPrincipals() != null)) {
|
|
142 |
3
| Iterator principals = subject.getPrincipals().iterator();
|
|
143 |
| |
|
144 |
3
| while (principals.hasNext()) {
|
|
145 |
3
| Principal p = (Principal) principals.next();
|
|
146 |
| |
|
147 |
3
| if (p instanceof Authentication) {
|
|
148 |
1
| return p;
|
|
149 |
| } |
|
150 |
| } |
|
151 |
| } |
|
152 |
| |
|
153 |
4
| return null;
|
|
154 |
| } |
|
155 |
| } |