|
| 1 | +package com.example.application.httprestserver.general.service.impl.config; |
| 2 | + |
| 3 | +import javax.inject.Inject; |
| 4 | +import javax.servlet.Filter; |
| 5 | + |
| 6 | +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; |
| 7 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 8 | +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| 9 | +import org.springframework.security.core.userdetails.UserDetailsService; |
| 10 | +import org.springframework.security.crypto.password.PasswordEncoder; |
| 11 | +import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler; |
| 12 | +import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler; |
| 13 | +import org.springframework.security.web.authentication.logout.LogoutFilter; |
| 14 | +import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler; |
| 15 | +import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; |
| 16 | +import org.springframework.security.web.util.matcher.AntPathRequestMatcher; |
| 17 | + |
| 18 | +import com.devonfw.module.security.common.api.config.WebSecurityConfigurer; |
| 19 | +import com.devonfw.module.security.common.impl.rest.AuthenticationSuccessHandlerSendingOkHttpStatusCode; |
| 20 | +import com.devonfw.module.security.common.impl.rest.JsonUsernamePasswordAuthenticationFilter; |
| 21 | +import com.devonfw.module.security.common.impl.rest.LogoutSuccessHandlerReturningOkHttpStatusCode; |
| 22 | + |
| 23 | +/** |
| 24 | + * This type serves as a base class for extensions of the {@code WebSecurityConfigurerAdapter} and provides a default |
| 25 | + * configuration. <br/> |
| 26 | + * Security configuration is based on {@link WebSecurityConfigurerAdapter}. This configuration is by purpose designed |
| 27 | + * most simple for two channels of authentication: simple login form and rest-url. |
| 28 | + */ |
| 29 | +public abstract class BaseWebSecurityConfig extends WebSecurityConfigurerAdapter { |
| 30 | + |
| 31 | + @Inject |
| 32 | + private UserDetailsService userDetailsService; |
| 33 | + |
| 34 | + @Inject |
| 35 | + private PasswordEncoder passwordEncoder; |
| 36 | + |
| 37 | + @Inject |
| 38 | + private WebSecurityConfigurer webSecurityConfigurer; |
| 39 | + |
| 40 | + /** |
| 41 | + * Configure spring security to enable a simple webform-login + a simple rest login. |
| 42 | + */ |
| 43 | + @Override |
| 44 | + public void configure(HttpSecurity http) throws Exception { |
| 45 | + |
| 46 | + String[] unsecuredResources = new String[] { "/login", "/security/**", "/services/rest/login", |
| 47 | + "/services/rest/logout" }; |
| 48 | + |
| 49 | + // disable CSRF protection by default, use csrf starter to override. |
| 50 | + http = http.csrf().disable(); |
| 51 | + // load starters as pluggins. |
| 52 | + http = this.webSecurityConfigurer.configure(http); |
| 53 | + |
| 54 | + http.httpBasic().and() |
| 55 | + // |
| 56 | + .userDetailsService(this.userDetailsService) |
| 57 | + // define all urls that are not to be secured |
| 58 | + .authorizeRequests().antMatchers(unsecuredResources).permitAll().anyRequest().authenticated().and() |
| 59 | + // configure parameters for simple form login (and logout) |
| 60 | + .formLogin().successHandler(new SimpleUrlAuthenticationSuccessHandler()).defaultSuccessUrl("/") |
| 61 | + .failureUrl("/login.html?error").loginProcessingUrl("/j_spring_security_login").usernameParameter("username") |
| 62 | + .passwordParameter("password").and() |
| 63 | + // logout via POST is possible |
| 64 | + .logout().logoutSuccessUrl("/login.html").and() |
| 65 | + // register login and logout filter that handles rest logins |
| 66 | + .addFilterAfter(getSimpleRestAuthenticationFilter(), BasicAuthenticationFilter.class) |
| 67 | + .addFilterAfter(getSimpleRestLogoutFilter(), LogoutFilter.class); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Create a simple filter that allows logout on a REST Url /services/rest/logout and returns a simple HTTP status 200 |
| 72 | + * ok. |
| 73 | + * |
| 74 | + * @return the filter. |
| 75 | + */ |
| 76 | + protected Filter getSimpleRestLogoutFilter() { |
| 77 | + |
| 78 | + LogoutFilter logoutFilter = new LogoutFilter(new LogoutSuccessHandlerReturningOkHttpStatusCode(), |
| 79 | + new SecurityContextLogoutHandler()); |
| 80 | + |
| 81 | + // configure logout for rest logouts |
| 82 | + logoutFilter.setLogoutRequestMatcher(new AntPathRequestMatcher("/services/rest/logout")); |
| 83 | + |
| 84 | + return logoutFilter; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Create a simple authentication filter for REST logins that reads user-credentials from a json-parameter and returns |
| 89 | + * status 200 instead of redirect after login. |
| 90 | + * |
| 91 | + * @return the {@link JsonUsernamePasswordAuthenticationFilter}. |
| 92 | + * @throws Exception if something goes wrong. |
| 93 | + */ |
| 94 | + protected JsonUsernamePasswordAuthenticationFilter getSimpleRestAuthenticationFilter() throws Exception { |
| 95 | + |
| 96 | + JsonUsernamePasswordAuthenticationFilter jsonFilter = new JsonUsernamePasswordAuthenticationFilter( |
| 97 | + new AntPathRequestMatcher("/services/rest/login")); |
| 98 | + jsonFilter.setPasswordParameter("j_password"); |
| 99 | + jsonFilter.setUsernameParameter("j_username"); |
| 100 | + jsonFilter.setAuthenticationManager(authenticationManager()); |
| 101 | + // set failurehandler that uses no redirect in case of login failure; just HTTP-status: 401 |
| 102 | + jsonFilter.setAuthenticationManager(authenticationManagerBean()); |
| 103 | + jsonFilter.setAuthenticationFailureHandler(new SimpleUrlAuthenticationFailureHandler()); |
| 104 | + // set successhandler that uses no redirect in case of login success; just HTTP-status: 200 |
| 105 | + jsonFilter.setAuthenticationSuccessHandler(new AuthenticationSuccessHandlerSendingOkHttpStatusCode()); |
| 106 | + return jsonFilter; |
| 107 | + } |
| 108 | + |
| 109 | + @SuppressWarnings("javadoc") |
| 110 | + @Inject |
| 111 | + public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { |
| 112 | + |
| 113 | + auth.inMemoryAuthentication().withUser("admin").password(this.passwordEncoder.encode("admin")).roles("Admin"); |
| 114 | + } |
| 115 | + |
| 116 | +} |
0 commit comments