Skip to content

Commit

Permalink
✨ feat(WebConfig.java, SecurityConfig.java): 添加日期时间格式配置与安全过滤链增强功能
Browse files Browse the repository at this point in the history
  • Loading branch information
vnobo committed Sep 26, 2024
1 parent 448570b commit a95ffad
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.plate.auth.config;

import jakarta.servlet.http.HttpSession;
import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -20,23 +21,49 @@


/**
* Provides the security configuration.
* @author Alex bob(<a href="https://github.com/vnobo">Alex Bob</a>)
*/

@Configuration(proxyBeanMethods = false)
@EnableJpaAuditing
public class SecurityConfig {

/**
* Provides a {@link PasswordEncoder} to be used for password storage.
* The {@link PasswordEncoder} is {@link org.springframework.security.crypto.factory.PasswordEncoderFactories#createDelegatingPasswordEncoder()}.
*
* @return a {@link PasswordEncoder} instance
*/
@Bean
public PasswordEncoder passwordEncoder() {
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}

/**
* Publishes {@link HttpSession} events to the Spring
* {@link org.springframework.context.ApplicationEventPublisher} so that
* {@link org.springframework.security.web.session.HttpSessionEventPublisher}
* can be used.
*
* @return an {@link HttpSessionEventPublisher} instance
*/
@Bean
public HttpSessionEventPublisher httpSessionEventPublisher() {
return new HttpSessionEventPublisher();
}

/**
* Configures the {@link SecurityFilterChain} to require authentication for all requests,
* except for static resources at common locations. The {@link SecurityFilterChain} uses
* HTTP Basic authentication and form login. The CSRF protection is enabled,
* and the logout URL is set to {@code /oauth/logout}. The logout handler is set to
* {@link HeaderWriterLogoutHandler} with a {@link ClearSiteDataHeaderWriter} to clear
* the cookies.
*
* @param http the {@link HttpSecurity} instance
* @return the configured {@link SecurityFilterChain}
* @throws Exception if an error occurs while configuring the {@link SecurityFilterChain}
*/
@Bean
public SecurityFilterChain springSecurity(HttpSecurity http) throws Exception {
HttpSessionRequestCache requestCache = new HttpSessionRequestCache();
Expand All @@ -50,7 +77,7 @@ public SecurityFilterChain springSecurity(HttpSecurity http) throws Exception {
.httpBasic(Customizer.withDefaults())
.formLogin((formLogin) -> formLogin.loginPage("/login").permitAll())
.csrf((csrf) -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()))
.logout((logout) -> logout.logoutUrl("/oauth2/logout")
.logout((logout) -> logout.logoutUrl("/oauth/logout")
.addLogoutHandler(new HeaderWriterLogoutHandler(new ClearSiteDataHeaderWriter(COOKIES))));
return http.build();
}
Expand Down
14 changes: 13 additions & 1 deletion boot/oauth2/src/main/java/com/plate/auth/config/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,19 @@ public class WebConfig implements WebMvcConfigurer {
private static final String DATE_FORMAT = "yyyy-MM-dd";
private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";


/**
* A Jackson2ObjectMapperBuilderCustomizer that configures the Jackson2ObjectMapperBuilder to use custom date and time
* formats for serialization and deserialization.
*
* <p>This customizer sets up the following formats for:
* <ul>
* <li>{@link java.time.LocalDate}: {@value #DATE_FORMAT}</li>
* <li>{@link java.time.LocalDateTime}: {@value #DATE_TIME_FORMAT}</li>
* </ul>
*
* @return A non-null Jackson2ObjectMapperBuilderCustomizer instance that configures the mapper with the custom date
* and time formats.
*/
@Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
return builder -> {
Expand Down

0 comments on commit a95ffad

Please sign in to comment.