2424
2525package org .sourcelab .kafka .webview .ui .configuration ;
2626
27+ import org .sourcelab .kafka .webview .ui .manager .user .AnonymousUserDetailsService ;
28+ import org .sourcelab .kafka .webview .ui .manager .user .CustomUserDetails ;
2729import org .sourcelab .kafka .webview .ui .manager .user .CustomUserDetailsService ;
2830import org .sourcelab .kafka .webview .ui .repository .UserRepository ;
2931import org .springframework .beans .factory .annotation .Autowired ;
30- import org .springframework .beans .factory .annotation .Value ;
3132import org .springframework .context .annotation .Bean ;
3233import org .springframework .context .annotation .Configuration ;
3334import org .springframework .security .config .annotation .authentication .builders .AuthenticationManagerBuilder ;
3940import org .springframework .security .web .util .matcher .AntPathRequestMatcher ;
4041import org .springframework .web .context .request .RequestContextListener ;
4142
43+ import java .util .ArrayList ;
44+
4245/**
4346 * Manages Security Configuration.
4447 */
@@ -49,12 +52,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
4952 @ Autowired
5053 private UserRepository userRepository ;
5154
52- /**
53- * Allows for requiring all requests over SSL.
54- * If not defined in the config under the key security.require_ssl, we default to false.
55- */
56- @ Value ("${app.require_ssl:false}" )
57- private boolean isRequireSsl ;
55+ @ Autowired
56+ private AppProperties appProperties ;
5857
5958 private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder ();
6059
@@ -65,43 +64,21 @@ public PasswordEncoder getPasswordEncoder() {
6564
6665 @ Override
6766 protected void configure (final HttpSecurity http ) throws Exception {
68- http
69- // CSRF Enabled
70- .csrf ().and ()
7167
72- .authorizeRequests ()
73- // Paths to static resources are available to anyone
74- .antMatchers ("/register/**" , "/login/**" , "/vendors/**" , "/css/**" , "/js/**" , "/img/**" )
75- .permitAll ()
76- // Users can edit their own profile
77- .antMatchers ("/configuration/user/edit/**" , "/configuration/user/update" )
78- .fullyAuthenticated ()
79- // But other Configuration paths require ADMIN role.
80- .antMatchers ("/configuration/**" )
81- .hasRole ("ADMIN" )
82- // All other requests must be authenticated
83- .anyRequest ()
84- .fullyAuthenticated ()
85- .and ()
86-
87- // Define how you login
88- .formLogin ()
89- .loginPage ("/login" )
90- .usernameParameter ("email" )
91- .passwordParameter ("password" )
92- .failureUrl ("/login?error=true" )
93- .defaultSuccessUrl ("/" )
94- .permitAll ()
95- .and ()
68+ // CSRF Enabled
69+ http
70+ .csrf ();
9671
97- // And how you logout
98- .logout ()
99- .logoutRequestMatcher (new AntPathRequestMatcher ("/logout" ))
100- .logoutSuccessUrl ("/login" )
101- .permitAll ();
72+ // If user auth is enabled
73+ if (appProperties .isUserAuthEnabled ()) {
74+ // Set it up.
75+ enableUserAuth (http );
76+ } else {
77+ disableUserAuth (http );
78+ }
10279
10380 // If require SSL is enabled
104- if (isRequireSsl ) {
81+ if (appProperties . isRequireSsl () ) {
10582 // Ensure its enabled.
10683 http
10784 .requiresChannel ()
@@ -112,10 +89,73 @@ protected void configure(final HttpSecurity http) throws Exception {
11289
11390 @ Override
11491 public void configure (final AuthenticationManagerBuilder auth ) throws Exception {
115- auth
116- // Define our custom user details service.
117- .userDetailsService (new CustomUserDetailsService (userRepository ))
118- .passwordEncoder (getPasswordEncoder ());
92+ if (appProperties .isUserAuthEnabled ()) {
93+ auth
94+ // Define our custom user details service.
95+ .userDetailsService (new CustomUserDetailsService (userRepository ))
96+ .passwordEncoder (getPasswordEncoder ());
97+ } else {
98+ auth
99+ // Define our custom user details service.
100+ .userDetailsService (new AnonymousUserDetailsService ());
101+ }
102+ }
103+
104+ /**
105+ * Sets up HttpSecurity for standard local user authentication.
106+ */
107+ private void enableUserAuth (final HttpSecurity http ) throws Exception {
108+ http
109+ .authorizeRequests ()
110+ // Paths to static resources are available to anyone
111+ .antMatchers ("/register/**" , "/login/**" , "/vendors/**" , "/css/**" , "/js/**" , "/img/**" )
112+ .permitAll ()
113+ // Users can edit their own profile
114+ .antMatchers ("/configuration/user/edit/**" , "/configuration/user/update" )
115+ .fullyAuthenticated ()
116+ // But other Configuration paths require ADMIN role.
117+ .antMatchers ("/configuration/**" )
118+ .hasRole ("ADMIN" )
119+ // All other requests must be authenticated
120+ .anyRequest ()
121+ .fullyAuthenticated ()
122+ .and ()
123+
124+ // Define how you login
125+ .formLogin ()
126+ .loginPage ("/login" )
127+ .usernameParameter ("email" )
128+ .passwordParameter ("password" )
129+ .failureUrl ("/login?error=true" )
130+ .defaultSuccessUrl ("/" )
131+ .permitAll ()
132+ .and ()
133+
134+ // And how you logout
135+ .logout ()
136+ .logoutRequestMatcher (new AntPathRequestMatcher ("/logout" ))
137+ .logoutSuccessUrl ("/login" )
138+ .permitAll ();
139+ }
140+
141+ /**
142+ * Sets up HttpSecurity for standard local user authentication.
143+ */
144+ private void disableUserAuth (final HttpSecurity http ) throws Exception {
145+ // Define the "User" that anonymous web clients will assume.
146+ final CustomUserDetails customUserDetails = AnonymousUserDetailsService .getDefaultAnonymousUser ();
147+
148+ http
149+ // All requests should require authorization as anonymous
150+ .authorizeRequests ()
151+ .anyRequest ()
152+ .anonymous ()
153+ .and ()
154+ // And the user provider should always return our anonymous user instance
155+ // with admin credentials.
156+ .anonymous ()
157+ .principal (customUserDetails )
158+ .authorities (new ArrayList <>(customUserDetails .getAuthorities ()));
119159 }
120160
121161 @ Bean
0 commit comments