Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions server/src/main/java/dev/findfirst/FindFirstApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ public FilterRegistrationBean<CorsFilter> simpleCorsFilter() {
config.setAllowCredentials(true);
// *** URL below needs to match the Vue client URL and port ***
// Local host and 127.0.0.1 are the same
config.setAllowedOrigins(Arrays.asList("https://localhost:3000", "http://localhost:3000",
"https://findfirst.dev", "http://localhost", "http://127.0.0.1"));
config.setAllowedOriginPatterns(Arrays.asList("https://localhost:3000", "http://localhost:3000",
"https://findfirst.dev", "http://localhost", "http://127.0.0.1",
"chrome-extension://*", "moz-extension://*"));
config.setAllowedMethods(Collections.singletonList("*"));
config.setAllowedHeaders(Collections.singletonList("*"));
source.registerCorsConfiguration("/**", config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@

import dev.findfirst.security.jwt.UserAuthenticationToken;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
import org.springframework.stereotype.Component;

@Component
public class UserContext {

public int getUserId() {
return ((UserAuthenticationToken) SecurityContextHolder.getContext().getAuthentication())
.getUserId();
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth instanceof UserAuthenticationToken uat) {
return uat.getUserId();
}
if (auth instanceof JwtAuthenticationToken jat) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am confused why we need this? The idea with code above is there is not a JWT since you already went through the JWT step of spring security?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This I need to explore tomorrow. I think its okay.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I really would rather have the one user authentication context but that would thinking about the JWT filter which I think we have but I would have to check.

Number userId = jat.getToken().getClaim("userId");
return userId.intValue();
}
throw new IllegalStateException("Unexpected authentication type: " + auth.getClass());
}
}
Loading