Hi bro, I have a spring boot project, just simply try to apply spring security with JWT. I have a config class that extends WebSecurityConfigurerAdapter, like this:
@Override
protected void configure(HttpSecurity http) throws Exception {
ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry = BLA BLA....;
registry.anyRequest().authenticated(); // require authentication for any endpoint that's not whitelisted`
http.addFilterAfter(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
}
You can see I added a filter to validate the token. And here is the filter:
Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
String jwt = tokenExtractor.extract(request);
if (StringUtils.hasText(jwt) && tokenVerifier.verify(jwt)) {
UserCredential user = tokenParser.getUserCredential(jwt, setting.getTokenSigningKey());
UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(
user, null, buildAuthorities(user));
SecurityContextHolder.getContext().setAuthentication(auth);
}
filterChain.doFilter(request, response);`
}
I integrated the bucket4j-spring-boot-starter with this config:
bucket4j.enabled=true
bucket4j.filters[0].cache-name=buckets
bucket4j.filters[0].filter-method=servlet
bucket4j.filters[0].url=.*
bucket4j.filters[0].rate-limits[0][email protected]() == null
bucket4j.filters[0].rate-limits[0].bandwidths[0].capacity=200
bucket4j.filters[0].rate-limits[0].bandwidths[0].time=60
bucket4j.filters[0].rate-limits[0].bandwidths[0].unit=seconds
bucket4j.filters[0].rate-limits[0].expression=getRemoteAddr()
In the SecurityService.class:
@Async
public String username() throws InterruptedException {
String result = null;
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); //NULL HERE
if(authentication != null && !authentication.getName().equalsIgnoreCase("anonymousUser")) {
result = authentication.getName();
}
System.out.println("username RETURN: " + result);
return result;
}
And here is the log - ALWAYS NULL

I tried to debug and found that this line
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return null. //Why???
I continued to debug and found that the method username() from SecurityService is invoked BEFORE the method doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) of the filter, so that the line SecurityContextHolder.getContext().setAuthentication(auth); is never be invoked => when I tried to get Authentication, always null here.
I kindly ask you to support me for this case. Many thanks
Hi bro, I have a spring boot project, just simply try to apply spring security with JWT. I have a config class that extends WebSecurityConfigurerAdapter, like this:
You can see I added a filter to validate the token. And here is the filter:
I integrated the bucket4j-spring-boot-starter with this config:
In the SecurityService.class:
And here is the log - ALWAYS NULL

I tried to debug and found that this line
I continued to debug and found that the method username() from SecurityService is invoked BEFORE the method doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) of the filter, so that the line SecurityContextHolder.getContext().setAuthentication(auth); is never be invoked => when I tried to get Authentication, always null here.
I kindly ask you to support me for this case. Many thanks