8
8
package io .lighty .aaa ;
9
9
10
10
import io .lighty .aaa .config .AAAConfiguration ;
11
- import io .lighty .server .LightyServerBuilder ;
11
+ import io .lighty .server .LightyJettyServerProvider ;
12
12
import java .util .ArrayList ;
13
13
import java .util .HashMap ;
14
14
import java .util .List ;
15
15
import java .util .Map ;
16
16
import java .util .concurrent .CompletableFuture ;
17
+ import javax .servlet .ServletException ;
17
18
import org .eclipse .jetty .server .Handler ;
18
- import org .eclipse .jetty .server .handler .ContextHandlerCollection ;
19
- import org .eclipse .jetty .servlet .FilterHolder ;
20
- import org .eclipse .jetty .servlet .ServletContextHandler ;
21
- import org .eclipse .jetty .servlet .ServletHolder ;
22
19
import org .eclipse .jetty .servlets .CrossOriginFilter ;
23
20
import org .glassfish .jersey .internal .guava .Preconditions ;
24
- import org .glassfish .jersey .server .ResourceConfig ;
25
- import org .glassfish .jersey .servlet .ServletContainer ;
26
21
import org .opendaylight .aaa .api .AuthenticationService ;
27
22
import org .opendaylight .aaa .api .ClaimCache ;
28
23
import org .opendaylight .aaa .api .CredentialAuth ;
44
39
import org .opendaylight .aaa .shiro .idm .IdmLightProxy ;
45
40
import org .opendaylight .aaa .shiro .moon .MoonTokenEndpoint ;
46
41
import org .opendaylight .aaa .shiro .web .env .AAAWebEnvironment ;
42
+ import org .opendaylight .aaa .shiro .web .env .ShiroWebContextSecurer ;
47
43
import org .opendaylight .aaa .tokenauthrealm .auth .AuthenticationManager ;
48
44
import org .opendaylight .aaa .tokenauthrealm .auth .HttpBasicAuth ;
49
45
import org .opendaylight .aaa .tokenauthrealm .auth .TokenAuthenticators ;
46
+ import org .opendaylight .aaa .web .FilterDetails ;
47
+ import org .opendaylight .aaa .web .ServletDetails ;
48
+ import org .opendaylight .aaa .web .WebContext ;
50
49
import org .opendaylight .aaa .web .servlet .jersey2 .JerseyServletSupport ;
51
50
import org .opendaylight .mdsal .binding .api .DataBroker ;
52
51
import org .opendaylight .yang .gen .v1 .urn .opendaylight .aaa .app .config .rev170619 .DatastoreConfig ;
53
52
import org .opendaylight .yang .gen .v1 .urn .opendaylight .aaa .app .config .rev170619 .ShiroConfiguration ;
54
53
import org .opendaylight .yang .gen .v1 .urn .opendaylight .aaa .password .service .config .rev170619 .PasswordServiceConfig ;
55
54
import org .opendaylight .yang .gen .v1 .urn .opendaylight .aaa .password .service .config .rev170619 .PasswordServiceConfigBuilder ;
55
+ import org .opendaylight .yangtools .concepts .Registration ;
56
56
import org .slf4j .Logger ;
57
57
import org .slf4j .LoggerFactory ;
58
58
@@ -62,7 +62,6 @@ public final class AAALightyShiroProvider {
62
62
63
63
private static AAALightyShiroProvider INSTANCE ;
64
64
65
- private final List <Handler > handlers ;
66
65
private final DataBroker dataBroker ;
67
66
private final ICertificateManager certificateManager ;
68
67
private final ShiroConfiguration shiroConfiguration ;
@@ -73,18 +72,19 @@ public final class AAALightyShiroProvider {
73
72
private ClaimCache claimCache ;
74
73
private PasswordHashService passwordHashService ;
75
74
private IIDMStore iidmStore ;
75
+ private Registration registration ;
76
+ private ShiroWebContextSecurer webContextSecurer ;
76
77
77
78
private AAAWebEnvironment aaaWebEnvironment ;
78
79
79
80
private AAALightyShiroProvider (final DataBroker dataBroker ,
80
81
final AAAConfiguration aaaConfiguration ,
81
82
final CredentialAuth <PasswordCredentials > credentialAuth ,
82
- final LightyServerBuilder server ) {
83
+ final LightyJettyServerProvider server ) {
83
84
this .dataBroker = dataBroker ;
84
85
this .certificateManager = aaaConfiguration .getCertificateManager ();
85
86
this .credentialAuth = credentialAuth ;
86
87
this .shiroConfiguration = aaaConfiguration .getShiroConf ();
87
- this .handlers = new ArrayList <>();
88
88
this .authenticationService = new AuthenticationManager ();
89
89
final DatastoreConfig datastoreConfig = aaaConfiguration .getDatastoreConf ();
90
90
@@ -127,46 +127,71 @@ private AAALightyShiroProvider(final DataBroker dataBroker,
127
127
initAAAonServer (server );
128
128
}
129
129
130
- private void initAAAonServer (final LightyServerBuilder server ) {
131
- final ContextHandlerCollection contexts = new ContextHandlerCollection ();
132
- final ServletContextHandler mainHandler = new ServletContextHandler (contexts , "/auth" , true , false );
133
- final IdmLightApplication idmLightApplication = new IdmLightApplication (iidmStore , claimCache );
134
- final ServletHolder idmLightServlet = new ServletHolder (new ServletContainer (ResourceConfig .forApplication (
135
- idmLightApplication )));
136
- idmLightServlet .setInitParameter ("jersey.config.server.provider.packages" ,
137
- "org.opendaylight.aaa.impl.provider" );
138
- mainHandler .addServlet (idmLightServlet , "/*" );
139
- server .addContextHandler (contexts );
140
- this .handlers .add (contexts );
141
- this .handlers .add (mainHandler );
142
- this .aaaWebEnvironment = new AAAWebEnvironment (shiroConfiguration ,
143
- dataBroker ,
144
- certificateManager ,
145
- authenticationService ,
146
- tokenAuthenticators ,
147
- passwordHashService ,
148
- new JerseyServletSupport ());
149
-
130
+ private void initAAAonServer (final LightyJettyServerProvider server ) {
150
131
final Map <String , String > properties = new HashMap <>();
151
132
final CustomFilterAdapterConfigurationImpl customFilterAdapterConfig =
152
- new CustomFilterAdapterConfigurationImpl ();
133
+ new CustomFilterAdapterConfigurationImpl ();
153
134
customFilterAdapterConfig .update (properties );
154
- final FilterHolder customFilterAdapter = new FilterHolder (new CustomFilterAdapter (customFilterAdapterConfig ));
155
- server .addCommonFilter (customFilterAdapter , "/*" );
156
135
157
- final FilterHolder shiroFilter = new FilterHolder (new AAAShiroFilter (aaaWebEnvironment ));
158
- server .addCommonFilter (shiroFilter , "/*" );
136
+ this .aaaWebEnvironment = new AAAWebEnvironment (
137
+ shiroConfiguration ,
138
+ dataBroker ,
139
+ certificateManager ,
140
+ authenticationService ,
141
+ tokenAuthenticators ,
142
+ passwordHashService ,
143
+ new JerseyServletSupport ()
144
+ );
145
+
146
+ final AAAShiroFilter aaaShiroFilter = new AAAShiroFilter (aaaWebEnvironment );
147
+
148
+ final var webContextBuilder = WebContext .builder ()
149
+ .name ("RealmManagement" )
150
+ .contextPath ("/auth" )
151
+ .supportsSessions (true )
152
+
153
+ // Add servlet
154
+ .addServlet (ServletDetails .builder ()
155
+ .servlet (new JerseyServletSupport ().createHttpServletBuilder (
156
+ new IdmLightApplication (iidmStore , claimCache )).build ())
157
+ .addUrlPattern ("/*" )
158
+ .build ())
159
+
160
+ // CustomFilterAdapter
161
+ .addFilter (FilterDetails .builder ()
162
+ .filter (new CustomFilterAdapter (customFilterAdapterConfig ))
163
+ .addUrlPattern ("/*" )
164
+ .build ())
165
+
166
+ // Shiro filter
167
+ .addFilter (FilterDetails .builder ()
168
+ .filter (aaaShiroFilter )
169
+ .addUrlPattern ("/*" )
170
+ .build ())
171
+
172
+ // CORS filter
173
+ .addFilter (FilterDetails .builder ()
174
+ .filter (new CrossOriginFilter ())
175
+ .addUrlPattern ("/*" )
176
+ .putInitParam ("allowedMethods" , "GET,POST,OPTIONS,DELETE,PUT,HEAD" )
177
+ .putInitParam ("allowedHeaders" , "origin, content-type, accept, authorization, Authorization" )
178
+ .build ());
159
179
160
- final FilterHolder crossOriginFilter = new FilterHolder (new CrossOriginFilter ());
161
- crossOriginFilter .setInitParameter ("allowedMethods" , "GET,POST,OPTIONS,DELETE,PUT,HEAD" );
162
- crossOriginFilter .setInitParameter ("allowedHeaders" ,
163
- "origin, content-type, accept, authorization, Authorization" );
164
- server .addCommonFilter (crossOriginFilter , "/*" );
180
+ this .webContextSecurer = new ShiroWebContextSecurer (aaaWebEnvironment );
181
+ this .webContextSecurer .requireAuthentication (webContextBuilder , "/*" , "/moon/*" );
182
+
183
+ aaaShiroFilter .init ();
184
+
185
+ try {
186
+ this .registration = server .build ().registerWebContext (webContextBuilder .build ());
187
+ } catch (ServletException e ) {
188
+ LOG .error ("Failed to register AAA web context: {}!" , server .getClass (), e );
189
+ }
165
190
}
166
191
167
192
public static CompletableFuture <AAALightyShiroProvider > newInstance (final DataBroker dataBroker ,
168
193
final AAAConfiguration aaaConfig , final CredentialAuth <PasswordCredentials > credentialAuth ,
169
- final LightyServerBuilder server ) {
194
+ final LightyJettyServerProvider server ) {
170
195
final CompletableFuture <AAALightyShiroProvider > completableFuture = new CompletableFuture <>();
171
196
INSTANCE = new AAALightyShiroProvider (dataBroker , aaaConfig , credentialAuth , server );
172
197
completableFuture .complete (INSTANCE );
@@ -225,6 +250,10 @@ public static IIDMStore getIdmStore() {
225
250
return INSTANCE .iidmStore ;
226
251
}
227
252
253
+ public ShiroWebContextSecurer getWebContextSecurer () {
254
+ return webContextSecurer ;
255
+ }
256
+
228
257
/**
229
258
* Set IDM data store, only used for test.
230
259
*
@@ -236,15 +265,7 @@ public static void setIdmStore(final IIDMStore store) {
236
265
237
266
@ SuppressWarnings ("IllegalCatch" )
238
267
public void close () {
239
- this .handlers .forEach ((handler ) -> {
240
- try {
241
- handler .stop ();
242
- } catch (Exception e ) {
243
- LOG .error ("Failed to close AAA handler [{}]" , handler , e );
244
- } finally {
245
- handler .destroy ();
246
- }
247
- });
268
+ registration .close ();
248
269
}
249
270
250
271
private static TokenAuthenticators buildTokenAuthenticators (
0 commit comments