Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit ec9dfc5b authored by Joze RIHTARSIC's avatar Joze RIHTARSIC
Browse files

Set csrf configuration

parent 665d8482
No related branches found
No related tags found
No related merge requests found
import {BrowserModule} from '@angular/platform-browser'; import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core'; import {NgModule} from '@angular/core';
import {FormsModule, ReactiveFormsModule} from '@angular/forms'; import {FormsModule, ReactiveFormsModule} from '@angular/forms';
import {HttpClient, HttpClientModule} from '@angular/common/http'; import {HttpClient, HttpClientModule, HttpClientXsrfModule} from '@angular/common/http';
import {FlexLayoutModule} from '@angular/flex-layout'; import {FlexLayoutModule} from '@angular/flex-layout';
import { import {
MatButtonModule, MatButtonModule,
...@@ -153,6 +153,10 @@ import {SmlIntegrationService} from "./domain/sml-integration.service"; ...@@ -153,6 +153,10 @@ import {SmlIntegrationService} from "./domain/sml-integration.service";
BrowserModule, BrowserModule,
FlexLayoutModule, FlexLayoutModule,
HttpClientModule, HttpClientModule,
HttpClientXsrfModule.withOptions({
cookieName: 'XSRF-TOKEN',
headerName: 'X-XSRF-TOKEN'
}),
BrowserAnimationsModule, BrowserAnimationsModule,
FormsModule, FormsModule,
NgxDatatableModule, NgxDatatableModule,
...@@ -200,7 +204,7 @@ import {SmlIntegrationService} from "./domain/sml-integration.service"; ...@@ -200,7 +204,7 @@ import {SmlIntegrationService} from "./domain/sml-integration.service";
provide: ExtendedHttpClient, provide: ExtendedHttpClient,
useFactory: extendedHttpClientCreator, useFactory: extendedHttpClientCreator,
deps: [HttpClient, HttpEventService, SecurityService] deps: [HttpClient, HttpEventService, SecurityService]
}, }
], ],
bootstrap: [AppComponent] bootstrap: [AppComponent]
}) })
......
...@@ -36,7 +36,6 @@ import org.springframework.security.config.annotation.web.builders.HttpSecurity; ...@@ -36,7 +36,6 @@ import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity; import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository; import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
import org.springframework.security.web.csrf.CsrfTokenRepository; import org.springframework.security.web.csrf.CsrfTokenRepository;
import org.springframework.security.web.firewall.DefaultHttpFirewall; import org.springframework.security.web.firewall.DefaultHttpFirewall;
...@@ -57,6 +56,8 @@ public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { ...@@ -57,6 +56,8 @@ public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
SMPAuthenticationProvider smpAuthenticationProvider; SMPAuthenticationProvider smpAuthenticationProvider;
BlueCoatAuthenticationFilter blueCoatAuthenticationFilter; BlueCoatAuthenticationFilter blueCoatAuthenticationFilter;
EDeliveryX509AuthenticationFilter x509AuthenticationFilter; EDeliveryX509AuthenticationFilter x509AuthenticationFilter;
CsrfTokenRepository csrfTokenRepository;
RequestMatcher csrfURLMatcher;
@Value("${authentication.blueCoat.enabled:false}") @Value("${authentication.blueCoat.enabled:false}")
boolean clientCertEnabled; boolean clientCertEnabled;
...@@ -73,11 +74,15 @@ public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { ...@@ -73,11 +74,15 @@ public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired @Autowired
public SpringSecurityConfig(SMPAuthenticationProvider smpAuthenticationProvider, public SpringSecurityConfig(SMPAuthenticationProvider smpAuthenticationProvider,
@Lazy BlueCoatAuthenticationFilter blueCoatAuthenticationFilter, @Lazy BlueCoatAuthenticationFilter blueCoatAuthenticationFilter,
@Lazy EDeliveryX509AuthenticationFilter x509AuthenticationFilter) { @Lazy EDeliveryX509AuthenticationFilter x509AuthenticationFilter,
@Lazy CsrfTokenRepository csrfTokenRepository,
@Lazy RequestMatcher csrfURLMatcher) {
super(false); super(false);
this.smpAuthenticationProvider = smpAuthenticationProvider; this.smpAuthenticationProvider = smpAuthenticationProvider;
this.blueCoatAuthenticationFilter = blueCoatAuthenticationFilter; this.blueCoatAuthenticationFilter = blueCoatAuthenticationFilter;
this.x509AuthenticationFilter = x509AuthenticationFilter; this.x509AuthenticationFilter = x509AuthenticationFilter;
this.csrfTokenRepository = csrfTokenRepository;
this.csrfURLMatcher = csrfURLMatcher;
} }
@Override @Override
...@@ -87,9 +92,7 @@ public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { ...@@ -87,9 +92,7 @@ public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
blueCoatAuthenticationFilter.setBlueCoatEnabled(clientCertEnabled); blueCoatAuthenticationFilter.setBlueCoatEnabled(clientCertEnabled);
httpSecurity httpSecurity
// .csrf().disable() .csrf().csrfTokenRepository(csrfTokenRepository).requireCsrfProtectionMatcher(csrfURLMatcher).and()
.csrf().csrfTokenRepository(tokenRepository()).requireCsrfProtectionMatcher(csrfURLMatcher()).and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.ALWAYS).and()
.exceptionHandling().authenticationEntryPoint(new SpringSecurityExceptionHandler()).and() .exceptionHandling().authenticationEntryPoint(new SpringSecurityExceptionHandler()).and()
.headers().frameOptions().deny().contentTypeOptions().and().xssProtection().xssProtectionEnabled(true).and().and() .headers().frameOptions().deny().contentTypeOptions().and().xssProtection().xssProtectionEnabled(true).and().and()
...@@ -161,26 +164,33 @@ public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { ...@@ -161,26 +164,33 @@ public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
} }
@Bean @Bean
public CsrfTokenRepository tokenRepository(){ public CsrfTokenRepository tokenRepository() {
CookieCsrfTokenRepository csrfTokenRepository = new CookieCsrfTokenRepository(); CookieCsrfTokenRepository repository = CookieCsrfTokenRepository.withHttpOnlyFalse();
csrfTokenRepository.setCookieHttpOnly(false); return repository;
return csrfTokenRepository;
} }
@Bean @Bean
public RequestMatcher csrfURLMatcher() { public RequestMatcher csrfURLMatcher() {
URLCsrfMatcher requestMatcher = new URLCsrfMatcher(); URLCsrfMatcher requestMatcher = new URLCsrfMatcher();
// init pages
requestMatcher.addIgnoreUrl("^/$", HttpMethod.GET);
requestMatcher.addIgnoreUrl("favicon.ico$", HttpMethod.GET);
requestMatcher.addIgnoreUrl("^/(index.html|ui/(#/)?|)$", HttpMethod.GET);
// Csrf ignore "SMP API 'stateless' calls! (each call is authenticated and session is not used!)" // Csrf ignore "SMP API 'stateless' calls! (each call is authenticated and session is not used!)"
requestMatcher.addIgnoreUrl("/.*::.*(/services/?.*)?", HttpMethod.GET, HttpMethod.DELETE, HttpMethod.POST, HttpMethod.PUT); requestMatcher.addIgnoreUrl("/.*::.*(/services/?.*)?", HttpMethod.GET, HttpMethod.DELETE, HttpMethod.POST, HttpMethod.PUT);
// ignore for login and logout // ignore for login and logout
requestMatcher.addIgnoreUrl("/ui/rest/security/authentication", HttpMethod.DELETE, HttpMethod.POST); requestMatcher.addIgnoreUrl("/ui/rest/security/authentication", HttpMethod.DELETE, HttpMethod.POST);
// info // allow all gets
requestMatcher.addIgnoreUrl("/ui/rest/application/(info|rootContext|name)", HttpMethod.GET); requestMatcher.addIgnoreUrl("/ui/.*", HttpMethod.GET);
// altternative fine tuned
//requestMatcher.addIgnoreUrl("/ui/(index.html|styles.*|runtime.*|polyfills.*|main.*)", HttpMethod.GET);
//requestMatcher.addIgnoreUrl("/ui/.*(\\.html|\\.css|\\.js)$", HttpMethod.GET);
//requestMatcher.addIgnoreUrl("/ui/assets/.*", HttpMethod.GET); // allow to retrieve assets
//requestMatcher.addIgnoreUrl("/ui/rest/(domain|search).*", HttpMethod.GET); // public methods
//requestMatcher.addIgnoreUrl("/ui/rest/application/(info|rootContext|name)", HttpMethod.GET); // public methods
// monitor // monitor
requestMatcher.addIgnoreUrl("/monitor/is-alive", HttpMethod.GET); requestMatcher.addIgnoreUrl("/monitor/is-alive", HttpMethod.GET);
// public search
requestMatcher.addIgnoreUrl("/ui/rest/search", HttpMethod.GET);
return requestMatcher; return requestMatcher;
} }
} }
...@@ -3,7 +3,6 @@ package eu.europa.ec.edelivery.smp.ui; ...@@ -3,7 +3,6 @@ package eu.europa.ec.edelivery.smp.ui;
import eu.europa.ec.edelivery.smp.auth.SMPAuthenticationService; import eu.europa.ec.edelivery.smp.auth.SMPAuthenticationService;
import eu.europa.ec.edelivery.smp.auth.SMPAuthenticationToken; import eu.europa.ec.edelivery.smp.auth.SMPAuthenticationToken;
import eu.europa.ec.edelivery.smp.auth.SMPAuthority;
import eu.europa.ec.edelivery.smp.auth.SMPAuthorizationService; import eu.europa.ec.edelivery.smp.auth.SMPAuthorizationService;
import eu.europa.ec.edelivery.smp.data.ui.ErrorRO; import eu.europa.ec.edelivery.smp.data.ui.ErrorRO;
import eu.europa.ec.edelivery.smp.data.ui.LoginRO; import eu.europa.ec.edelivery.smp.data.ui.LoginRO;
...@@ -22,6 +21,8 @@ import org.springframework.security.core.AuthenticationException; ...@@ -22,6 +21,8 @@ import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.CookieClearingLogoutHandler; import org.springframework.security.web.authentication.logout.CookieClearingLogoutHandler;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler; import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.security.web.csrf.CsrfToken;
import org.springframework.security.web.csrf.CsrfTokenRepository;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
...@@ -54,6 +55,9 @@ public class AuthenticationResource { ...@@ -54,6 +55,9 @@ public class AuthenticationResource {
@Autowired @Autowired
private ConfigurationService configurationService; private ConfigurationService configurationService;
@Autowired
public CsrfTokenRepository csrfTokenRepository;
SMPCookieWriter smpCookieWriter = new SMPCookieWriter(); SMPCookieWriter smpCookieWriter = new SMPCookieWriter();
...@@ -68,9 +72,10 @@ public class AuthenticationResource { ...@@ -68,9 +72,10 @@ public class AuthenticationResource {
@Transactional(noRollbackFor = BadCredentialsException.class) @Transactional(noRollbackFor = BadCredentialsException.class)
public UserRO authenticate(@RequestBody LoginRO loginRO, HttpServletRequest request, HttpServletResponse response) { public UserRO authenticate(@RequestBody LoginRO loginRO, HttpServletRequest request, HttpServletResponse response) {
LOG.debug("Authenticating user [{}]", loginRO.getUsername()); LOG.debug("Authenticating user [{}]", loginRO.getUsername());
// reset session id with login // reset session id token and the Csrf Token at login
recreatedSessionCookie(request, response); recreatedSessionCookie(request, response);
CsrfToken csfrToken = csrfTokenRepository.generateToken(request);
csrfTokenRepository.saveToken(csfrToken, request, response);
SMPAuthenticationToken authentication = (SMPAuthenticationToken) authenticationService.authenticate(loginRO.getUsername(), loginRO.getPassword()); SMPAuthenticationToken authentication = (SMPAuthenticationToken) authenticationService.authenticate(loginRO.getUsername(), loginRO.getPassword());
UserRO userRO = conversionService.convert(authentication.getUser(), UserRO.class); UserRO userRO = conversionService.convert(authentication.getUser(), UserRO.class);
...@@ -111,6 +116,8 @@ public class AuthenticationResource { ...@@ -111,6 +116,8 @@ public class AuthenticationResource {
* @param response * @param response
*/ */
public void recreatedSessionCookie(HttpServletRequest request, HttpServletResponse response) { public void recreatedSessionCookie(HttpServletRequest request, HttpServletResponse response) {
// recreate session id (first make sure it exists)
request.getSession(true).getId();
String sessionId = request.changeSessionId(); String sessionId = request.changeSessionId();
smpCookieWriter.writeCookieToResponse(SESSION_COOKIE_NAME, smpCookieWriter.writeCookieToResponse(SESSION_COOKIE_NAME,
sessionId, sessionId,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment