Skip to content

Commit

Permalink
Feat: add MockSpringSecurityFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
emost22 committed Jul 13, 2024
1 parent b8e5d25 commit ec59b41
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ dependencies {
compileOnly('com.h2database:h2:2.1.214')

implementation 'org.springframework.boot:spring-boot-starter-security'
testImplementation 'org.springframework.security:spring-security-test'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'

implementation group: 'com.auth0', name: 'java-jwt', version: '4.2.1'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.sirius.spurt.common.jwt;

import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.FilterConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import java.io.IOException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class MockSpringSecurityFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) {}

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException, ServletException {
SecurityContextHolder.getContext()
.setAuthentication((Authentication) ((HttpServletRequest) req).getUserPrincipal());
chain.doFilter(req, res);
}

@Override
public void destroy() {
SecurityContextHolder.clearContext();
}
}

0 comments on commit ec59b41

Please sign in to comment.