Skip to content

Commit bfe9739

Browse files
committed
Make FileDTO Serializable, enable Thymeleaf cache in production, use Jackson ObjectWriter and other minor cleanup
WE2-860 Signed-off-by: Mart Somermaa <[email protected]>
1 parent 05e6269 commit bfe9739

File tree

9 files changed

+44
-44
lines changed

9 files changed

+44
-44
lines changed

example/src/main/java/eu/webeid/example/config/ApplicationConfiguration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
3232
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
3333
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
34+
import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer;
3435
import org.springframework.security.web.SecurityFilterChain;
3536
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
3637
import org.springframework.security.web.authentication.logout.HttpStatusReturningLogoutSuccessHandler;
@@ -65,7 +66,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
6566
.addFilterBefore(new WebEidAjaxLoginProcessingFilter("/auth/login", manager, securityContextRepository),
6667
UsernamePasswordAuthenticationFilter.class)
6768
.logout(logout -> logout.logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler()))
68-
.headers(headers -> headers.frameOptions(options -> options.sameOrigin()))
69+
.headers(headers -> headers.frameOptions(HeadersConfigurer.FrameOptionsConfig::sameOrigin))
6970
.build();
7071
}
7172

example/src/main/java/eu/webeid/example/security/AuthTokenDTOAuthenticationProvider.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323
package eu.webeid.example.security;
2424

2525
import eu.webeid.example.security.dto.AuthTokenDTO;
26+
import eu.webeid.security.authtoken.WebEidAuthToken;
27+
import eu.webeid.security.challenge.ChallengeNonceStore;
28+
import eu.webeid.security.exceptions.AuthTokenException;
29+
import eu.webeid.security.validator.AuthTokenValidator;
2630
import org.slf4j.Logger;
2731
import org.slf4j.LoggerFactory;
28-
import org.springframework.beans.factory.annotation.Autowired;
2932
import org.springframework.security.authentication.AuthenticationProvider;
3033
import org.springframework.security.authentication.AuthenticationServiceException;
3134
import org.springframework.security.core.Authentication;
@@ -34,15 +37,9 @@
3437
import org.springframework.security.core.authority.SimpleGrantedAuthority;
3538
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
3639
import org.springframework.stereotype.Component;
37-
import eu.webeid.security.authtoken.WebEidAuthToken;
38-
import eu.webeid.security.challenge.ChallengeNonceStore;
39-
import eu.webeid.security.exceptions.AuthTokenException;
40-
import eu.webeid.security.validator.AuthTokenValidator;
4140

4241
import java.security.cert.CertificateEncodingException;
4342
import java.security.cert.X509Certificate;
44-
import java.util.ArrayList;
45-
import java.util.Arrays;
4643
import java.util.Collections;
4744
import java.util.List;
4845

@@ -56,10 +53,13 @@ public class AuthTokenDTOAuthenticationProvider implements AuthenticationProvide
5653

5754
private static final Logger LOG = LoggerFactory.getLogger(AuthTokenDTOAuthenticationProvider.class);
5855

59-
@Autowired
60-
private AuthTokenValidator tokenValidator;
61-
@Autowired
62-
private ChallengeNonceStore challengeNonceStore;
56+
private final AuthTokenValidator tokenValidator;
57+
private final ChallengeNonceStore challengeNonceStore;
58+
59+
public AuthTokenDTOAuthenticationProvider(AuthTokenValidator tokenValidator, ChallengeNonceStore challengeNonceStore) {
60+
this.tokenValidator = tokenValidator;
61+
this.challengeNonceStore = challengeNonceStore;
62+
}
6363

6464
@Override
6565
public Authentication authenticate(Authentication auth) throws AuthenticationException {

example/src/main/java/eu/webeid/example/security/WebEidAjaxLoginProcessingFilter.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,14 @@
2323
package eu.webeid.example.security;
2424

2525
import com.fasterxml.jackson.databind.ObjectMapper;
26-
import java.io.IOException;
27-
import jakarta.servlet.http.HttpServletRequest;
28-
import jakarta.servlet.http.HttpServletResponse;
29-
26+
import com.fasterxml.jackson.databind.ObjectReader;
3027
import eu.webeid.example.security.ajax.AjaxAuthenticationFailureHandler;
3128
import eu.webeid.example.security.ajax.AjaxAuthenticationSuccessHandler;
3229
import eu.webeid.example.security.dto.AuthTokenDTO;
3330
import jakarta.servlet.FilterChain;
3431
import jakarta.servlet.ServletException;
32+
import jakarta.servlet.http.HttpServletRequest;
33+
import jakarta.servlet.http.HttpServletResponse;
3534
import org.slf4j.Logger;
3635
import org.slf4j.LoggerFactory;
3736
import org.springframework.http.HttpMethod;
@@ -45,14 +44,17 @@
4544
import org.springframework.security.web.authentication.session.SessionFixationProtectionStrategy;
4645
import org.springframework.security.web.context.SecurityContextRepository;
4746

47+
import java.io.IOException;
48+
4849
public class WebEidAjaxLoginProcessingFilter extends AbstractAuthenticationProcessingFilter {
4950
private static final Logger LOG = LoggerFactory.getLogger(WebEidAjaxLoginProcessingFilter.class);
51+
private final ObjectReader OBJECT_READER = new ObjectMapper().readerFor(AuthTokenDTO.class);
5052
private final SecurityContextRepository securityContextRepository;
5153

5254
public WebEidAjaxLoginProcessingFilter(
53-
String defaultFilterProcessesUrl,
54-
AuthenticationManager authenticationManager,
55-
SecurityContextRepository securityContextRepository
55+
String defaultFilterProcessesUrl,
56+
AuthenticationManager authenticationManager,
57+
SecurityContextRepository securityContextRepository
5658
) {
5759
super(defaultFilterProcessesUrl);
5860
this.setAuthenticationManager(authenticationManager);
@@ -64,7 +66,7 @@ public WebEidAjaxLoginProcessingFilter(
6466

6567
@Override
6668
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
67-
throws AuthenticationException, IOException {
69+
throws AuthenticationException, IOException {
6870
if (!HttpMethod.POST.name().equals(request.getMethod())) {
6971
LOG.warn("HttpMethod not supported: {}", request.getMethod());
7072
throw new AuthenticationServiceException("HttpMethod not supported: " + request.getMethod());
@@ -76,8 +78,7 @@ public Authentication attemptAuthentication(HttpServletRequest request, HttpServ
7678
}
7779

7880
LOG.info("attemptAuthentication(): Reading request body");
79-
final ObjectMapper objectMapper = new ObjectMapper();
80-
final AuthTokenDTO authTokenDTO = objectMapper.readValue(request.getReader(), AuthTokenDTO.class);
81+
final AuthTokenDTO authTokenDTO = OBJECT_READER.readValue(request.getReader());
8182
LOG.info("attemptAuthentication(): Creating token");
8283
final PreAuthenticatedAuthenticationToken token = new PreAuthenticatedAuthenticationToken(null, authTokenDTO);
8384
LOG.info("attemptAuthentication(): Calling authentication manager");
@@ -86,7 +87,7 @@ public Authentication attemptAuthentication(HttpServletRequest request, HttpServ
8687

8788
@Override
8889
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException {
89-
super.successfulAuthentication(request, response, chain, authResult); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/OverriddenMethodBody
90+
super.successfulAuthentication(request, response, chain, authResult);
9091
securityContextRepository.saveContext(SecurityContextHolder.getContext(), request, response);
9192
}
9293
}

example/src/main/java/eu/webeid/example/security/ajax/AjaxAuthenticationSuccessHandler.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,17 @@
2525
import com.fasterxml.jackson.annotation.JsonProperty;
2626
import com.fasterxml.jackson.core.JsonProcessingException;
2727
import com.fasterxml.jackson.databind.ObjectMapper;
28-
import java.io.IOException;
29-
import java.util.Collection;
30-
import java.util.List;
31-
import java.util.stream.Collectors;
28+
import com.fasterxml.jackson.databind.ObjectWriter;
3229
import jakarta.servlet.http.HttpServletRequest;
3330
import jakarta.servlet.http.HttpServletResponse;
3431
import org.slf4j.Logger;
3532
import org.slf4j.LoggerFactory;
3633
import org.springframework.security.core.Authentication;
37-
import org.springframework.security.core.GrantedAuthority;
3834
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
3935
import org.springframework.stereotype.Component;
4036

37+
import java.io.IOException;
38+
4139
/**
4240
* Write custom response on having user successfully authenticated.
4341
* <p>
@@ -50,11 +48,11 @@ public class AjaxAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuc
5048

5149
@Override
5250
public void onAuthenticationSuccess(
53-
HttpServletRequest request,
54-
HttpServletResponse response,
55-
Authentication authentication
51+
HttpServletRequest request,
52+
HttpServletResponse response,
53+
Authentication authentication
5654
)
57-
throws IOException {
55+
throws IOException {
5856
LOG.info("onAuthenticationSuccess(): {}", authentication);
5957

6058
response.setStatus(HttpServletResponse.SC_OK);
@@ -64,23 +62,19 @@ public void onAuthenticationSuccess(
6462
}
6563

6664
public static class AuthSuccessDTO {
67-
private final ObjectMapper objectMapper = new ObjectMapper();
65+
private static final ObjectWriter OBJECT_WRITER = new ObjectMapper().writerFor(AuthSuccessDTO.class);
6866

6967
@JsonProperty("sub")
7068
private String sub;
7169

7270
@JsonProperty("auth")
73-
private List<String> auth;
71+
private String auth;
7472

7573
public static String asJson(Authentication authentication) throws JsonProcessingException {
7674
final AuthSuccessDTO dto = new AuthSuccessDTO();
7775
dto.sub = authentication.getName();
78-
dto.auth = convertAuthorities(authentication.getAuthorities());
79-
return dto.objectMapper.writeValueAsString(dto);
80-
}
81-
82-
private static List<String> convertAuthorities(Collection<? extends GrantedAuthority> authorities) {
83-
return authorities.stream().map(GrantedAuthority::toString).collect(Collectors.toList());
76+
dto.auth = authentication.getAuthorities().toString();
77+
return OBJECT_WRITER.writeValueAsString(dto);
8478
}
8579
}
8680
}

example/src/main/java/eu/webeid/example/service/SigningService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public class SigningService {
6464
private static final Logger LOG = LoggerFactory.getLogger(SigningService.class);
6565
private final Configuration signingConfiguration;
6666

67-
ObjectFactory<HttpSession> httpSessionFactory;
67+
private final ObjectFactory<HttpSession> httpSessionFactory;
6868

6969
public SigningService(ObjectFactory<HttpSession> httpSessionFactory, YAMLConfig yamlConfig) {
7070
this.httpSessionFactory = httpSessionFactory;

example/src/main/java/eu/webeid/example/service/dto/FileDTO.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@
2727
import org.springframework.web.multipart.MultipartFile;
2828

2929
import java.io.IOException;
30+
import java.io.Serializable;
3031
import java.net.URI;
3132
import java.nio.file.Files;
3233
import java.nio.file.Paths;
3334
import java.util.Objects;
3435

35-
public class FileDTO {
36+
public class FileDTO implements Serializable {
3637
private static final String EXAMPLE_FILENAME = "example-for-signing.txt";
3738

3839
private final String name;

example/src/main/java/eu/webeid/example/web/rest/ChallengeController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
package eu.webeid.example.web.rest;
2424

2525
import eu.webeid.example.service.dto.ChallengeDTO;
26+
import eu.webeid.security.challenge.ChallengeNonceGenerator;
2627
import org.springframework.web.bind.annotation.GetMapping;
2728
import org.springframework.web.bind.annotation.RequestMapping;
2829
import org.springframework.web.bind.annotation.RestController;
29-
import eu.webeid.security.challenge.ChallengeNonceGenerator;
3030

3131
@RestController
3232
@RequestMapping("auth")

example/src/main/resources/application-prod.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ web-eid-auth-token:
33
use-digidoc4j-prod-configuration: true
44
local-origin: "https://web-eid.eu"
55
truststore-password: "changeit"
6+
spring:
7+
thymeleaf:
8+
cache: true

example/src/test/java/eu/webeid/example/WebApplicationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public void validateOcspResponse(XadesSignature xadesSignature) {
110110
MvcResult result = HttpHelper.login(mvcBuilder, session, ObjectMother.mockAuthToken());
111111
session = (MockHttpSession) result.getRequest().getSession();
112112
MockHttpServletResponse response = result.getResponse();
113-
assertEquals("{\"sub\":\"JAAK-KRISTJAN JÕEORG\",\"auth\":[\"ROLE_USER\"]}", response.getContentAsString());
113+
assertEquals("{\"sub\":\"JAAK-KRISTJAN JÕEORG\",\"auth\":\"[ROLE_USER]\"}", response.getContentAsString());
114114

115115
/* Example how to test file upload.
116116
response = HttpHelper.upload(mvcBuilder, session, mockMultipartFile());

0 commit comments

Comments
 (0)