SpringBoot (10) Authentication (1). Server certificate, application.yml and Application.java
0. Introduction
The goal is to use a certificate for client authentication. So the HTTPS protocol should be used.
To use this protocol, a server certificate is needed.
Let's see the steps to accomplish this target:
1. Server certificate
Get a server certificate in "p12" format with a password.
Create a folder (keystores) in src/main/resources and place the server certificate in this folder.
2. application.yml file
Here is the file
======================================================================
server: #==================================================================================== # 1. For accepting "{ }" in parameters and for accepting a long number of parameters #==================================================================================== #@see https://stackoverflow.com/a/58440058/7704658 #@see https://www.programmersought.com/article/4517808578/ tomcat: relaxed-query-chars: ['{','}'] max-parameter-count: -1 #==================================================================================== # 2. SSL-X05 Authorisation #==================================================================================== #--ssl-x05 autorization @ see https://www.baeldung.com/x-509-authentication-in-spring-security ssl: key-store: classpath:keystores/server_cert.p12 keyStoreType: pkcs12 key-store-password: myPasword client-auth: need port: 8443 #==================================================================================== # 3. Include stracktrace in the error page #==================================================================================== #--error management #--@see https://www.logicbig.com/tutorials/spring-framework/spring-boot/custom-thymeleaf-error-page.html error:
include-stacktrace: always
======================================================================
The file is commented on. The SSL configuration needs the server certificate, its password, the type of keystore, and the param client-auth set to "need".
2. Application.java file
Here is the file:
=====================================================================
package ximo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.core.authority.AuthorityUtils; import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.web.SecurityFilterChain; import ximo.xotherapps.utils.basic.CertificateUtils; @SpringBootApplication @EnableWebSecurity public class Application { /************************************************************** * 1. CERTIFICATE MANAGEMENT **************************************************************/ @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http .authorizeHttpRequests() .anyRequest() .authenticated() .and() .x509() .subjectPrincipalRegex("CN=(.*?)(?:,|$)") .userDetailsService(userDetailsService()); return http.build(); } /**************************************************************** * 2. GET USER DETAILS *****************************************************************/ @Bean public UserDetailsService userDetailsService() { return new UserDetailsService() { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { System.out.println("username="+username); //CertificateUtils is a my class for managing certificates String DNI=CertificateUtils.getDNIFromCN(username); System.out.println("DNI="+DNI); //Assign ROLE_USER to all users that use a certificate return new User(username, "", AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER")); } }; } public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
======================================================================
Comentarios
Publicar un comentario