JAVA-29315: Upgrade spring-security-web-react to parent-spring-6 (#15920)

This commit is contained in:
Harry9656 2024-02-20 01:19:05 +01:00 committed by GitHub
parent c0756a59b7
commit bd36eddeca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 43 additions and 45 deletions

View File

@ -10,9 +10,9 @@
<parent> <parent>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<artifactId>parent-spring-5</artifactId> <artifactId>parent-spring-6</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-spring-5</relativePath> <relativePath>../../parent-spring-6</relativePath>
</parent> </parent>
<dependencies> <dependencies>
@ -20,17 +20,17 @@
<dependency> <dependency>
<groupId>org.springframework.security</groupId> <groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId> <artifactId>spring-security-web</artifactId>
<version>${spring-security.version}</version> <version>${spring.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.security</groupId> <groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId> <artifactId>spring-security-config</artifactId>
<version>${spring-security.version}</version> <version>${spring.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.security</groupId> <groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId> <artifactId>spring-security-taglibs</artifactId>
<version>${spring-security.version}</version> <version>${spring.version}</version>
</dependency> </dependency>
<!-- Spring --> <!-- Spring -->
<dependency> <dependency>
@ -65,22 +65,21 @@
</dependency> </dependency>
<!-- web --> <!-- web -->
<dependency> <dependency>
<groupId>javax.servlet</groupId> <groupId>jakarta.servlet</groupId>
<artifactId>javax.servlet-api</artifactId> <artifactId>jakarta.servlet-api</artifactId>
<version>${javax.servlet-api.version}</version> <version>${jakarta-servlet-api.version}</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>javax.servlet</groupId> <groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jstl</artifactId> <artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
<version>${jstl.version}</version> <version>${jakarta-servlet-jsp.version}</version>
<scope>runtime</scope>
</dependency> </dependency>
<!-- Test --> <!-- Test -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot-starter-test.version}</version> <version>${spring-boot.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
@ -153,6 +152,8 @@
<!-- frontend --> <!-- frontend -->
<node.version>v8.11.3</node.version> <node.version>v8.11.3</node.version>
<npm.version>6.1.0</npm.version> <npm.version>6.1.0</npm.version>
<jakarta-servlet-jsp.version>3.0.0</jakarta-servlet-jsp.version>
<jakarta-servlet-api.version>6.1.0-M1</jakarta-servlet-api.version>
</properties> </properties>
</project> </project>

View File

@ -1,5 +1,4 @@
package com.baeldung.spring; package com.baeldung.spring;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -10,6 +9,8 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import jakarta.servlet.http.HttpServletRequest;
@Controller @Controller
@RequestMapping("/rest") @RequestMapping("/rest")
public class RestController { public class RestController {

View File

@ -38,28 +38,21 @@ public class SecSecurityConfig {
@Bean @Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.csrf() return http.csrf(csrf -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()))
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) .authorizeHttpRequests(request -> request.requestMatchers("/admin/**")
.and() .hasRole("ADMIN")
.authorizeRequests() .requestMatchers("/anonymous*")
.antMatchers("/admin/**") .anonymous()
.hasRole("ADMIN") .requestMatchers(HttpMethod.GET, "/index*", "/static/**", "/*.js", "/*.json", "/*.ico", "/rest")
.antMatchers("/anonymous*") .permitAll()
.anonymous() .anyRequest()
.antMatchers(HttpMethod.GET, "/index*", "/static/**", "/*.js", "/*.json", "/*.ico", "/rest") .authenticated())
.permitAll() .formLogin(form -> form.loginPage("/index.html")
.anyRequest() .loginProcessingUrl("/perform_login")
.authenticated() .defaultSuccessUrl("/homepage.html", true)
.and() .failureUrl("/index.html?error=true"))
.formLogin() .logout(logout -> logout.logoutUrl("/perform_logout")
.loginPage("/index.html") .deleteCookies("JSESSIONID"))
.loginProcessingUrl("/perform_login") .build();
.defaultSuccessUrl("/homepage.html", true)
.failureUrl("/index.html?error=true")
.and()
.logout()
.logoutUrl("/perform_logout")
.deleteCookies("JSESSIONID");
return http.build();
} }
} }

View File

@ -1,19 +1,22 @@
package com.baeldung; package com.baeldung;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.web.WebAppConfiguration;
import com.baeldung.spring.MvcConfig; import com.baeldung.spring.MvcConfig;
import com.baeldung.spring.SecSecurityConfig; import com.baeldung.spring.SecSecurityConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration @WebAppConfiguration
@ContextConfiguration(classes = { MvcConfig.class, SecSecurityConfig.class }) @ContextConfiguration(classes = { MvcConfig.class, SecSecurityConfig.class })
public class SpringContextTest { @ExtendWith(SpringExtension.class)
class SpringContextTest {
@Test @Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() { void whenSpringContextIsBootstrapped_thenNoExceptions() {
} }
} }