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