At the moment, I have SpringBoot 3.3.0-SPANSHOT installed. But my old version is written in the code – 2.7.5. My goal is to update the latest version of SpringBoot and redo the code in a new way (except for authorization/registration). But I decided to copy the authorization/registration (JWT) from an old project that is not stable.
JwtConfigurer.java
package com.pzh.WebCore.security.jwt;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
public class JwtConfigurer extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {
private JwtTokenProvider jwtTokenProvider;
public JwtConfigurer(JwtTokenProvider jwtTokenProvider) {
this.jwtTokenProvider = jwtTokenProvider;
}
@Override
public void configure(HttpSecurity httpSecurity) throws Exception {
JwtTokenFilter jwtTokenFilter = new JwtTokenFilter(jwtTokenProvider);
httpSecurity.addFilterBefore(jwtTokenFilter, UsernamePasswordAuthenticationFilter.class);
}
}
JwtTokenFilter.java
package com.pzh.WebCore.security.jwt;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.filter.GenericFilterBean;
import java.io.IOException;
public class JwtTokenFilter extends GenericFilterBean {
private JwtTokenProvider jwtTokenProvider;
public JwtTokenFilter(JwtTokenProvider jwtTokenProvider) {
this.jwtTokenProvider = jwtTokenProvider;
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain)
throws IOException, ServletException {
String token = jwtTokenProvider.resolveToken((HttpServletRequest) req);
if (token != null && jwtTokenProvider.validateToken(token)) {
Authentication auth = jwtTokenProvider.getAuthentication(token);
if (auth != null) {
SecurityContextHolder.getContext().setAuthentication(auth);
}
}
filterChain.doFilter(req, res);
}
}
build.gradle
plugins {
id 'java'
id 'org.springframework.boot' version '3.3.0-SNAPSHOT'
id 'io.spring.dependency-management' version '1.1.4'
}
group = 'com.pzh'
version = '1.0'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
java {
sourceCompatibility = '17'
}
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
maven { url 'https://repo.spring.io/snapshot' }
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-mail:2.7.5'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.liquibase:liquibase-core'
implementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'junit:junit:4.13.1'
implementation 'io.jsonwebtoken:jjwt-api:0.11.5'
implementation 'io.jsonwebtoken:jjwt-gson:0.11.5'
implementation 'mysql:mysql-connector-java:8.0.26'
implementation 'org.springframework.security:spring-security-config:4.1.3.RELEASE'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
runtimeOnly 'mysql:mysql-connector-java'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.5'
compileOnly 'org.projectlombok:lombok'
compileOnly 'javax.servlet:javax.servlet-api:4.0.1'
compileOnly files("libs/jjwt-impl-0.11.5.jar")
compileOnly files("libs/jjwt-jackson-0.11.5.jar")
annotationProcessor 'org.projectlombok:lombok'
}
tasks.named('test') {
useJUnitPlatform()
}