-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JSON login back; remove submodule cloning being necessary, as we …
…now use the git reference to material-table
- Loading branch information
Ariel Guelfi
committed
Mar 20, 2024
1 parent
be9a395
commit 3af6519
Showing
6 changed files
with
63 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/java/pl/databucket/server/security/JsonAuthenticationFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package pl.databucket.server.security; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
import javax.servlet.http.HttpServletRequest; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.web.authentication.AuthenticationSuccessHandler; | ||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
import org.springframework.security.web.util.matcher.AntPathRequestMatcher; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* Allows for a POST JSON sigin in instead of the x-www-form-urlencoded version when calling POST /login-form | ||
*/ | ||
@Component | ||
public class JsonAuthenticationFilter extends UsernamePasswordAuthenticationFilter { | ||
|
||
private final ObjectMapper mapper; | ||
/** | ||
* We can't call request.getReader() twice, so we save the result of the first call in a ThreadLocal var as not to | ||
* mix different requests. | ||
*/ | ||
private static final ThreadLocal<Map<String, String>> ongoingAuth = new ThreadLocal<>(); | ||
|
||
public JsonAuthenticationFilter(ObjectMapper mapper, AuthenticationManager authenticationManager, | ||
AuthenticationSuccessHandler successHandler) { | ||
super(authenticationManager); | ||
this.setAuthenticationSuccessHandler(successHandler); | ||
this.setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher("/api/public/sign-in", | ||
"POST")); | ||
this.mapper = mapper; | ||
} | ||
|
||
@Override | ||
protected String obtainUsername(HttpServletRequest request) { | ||
try { | ||
Map<String, String> map = mapper.readValue(request.getReader(), new TypeReference<>() { | ||
}); | ||
ongoingAuth.set(map); | ||
return map.get(SPRING_SECURITY_FORM_USERNAME_KEY); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
protected String obtainPassword(HttpServletRequest request) { | ||
return ongoingAuth.get().get(SPRING_SECURITY_FORM_PASSWORD_KEY); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters