Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add logging of 403 and 401 errors #3138

Merged
merged 3 commits into from
Mar 24, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static boolean isDirty(final User a, final User b) {
* @return
*/
private static int hash(final User user) {
return (user.id + user.username + user.email + user.givenName + user.familyName + user.name + user.enabled).hashCode();
return (user.id + user.username + user.email + user.givenName + user.familyName + user.name + user.enabled + user.roles).hashCode();
}

public User merge(final User other) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package software.uncharted.terarium.hmiserver.security;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import java.io.IOException;

@Slf4j
public class LoggingAccessDeniedHandler implements AccessDeniedHandler {
@Override
public void handle(final HttpServletRequest request, final HttpServletResponse response, final AccessDeniedException accessDeniedException) throws IOException, ServletException {
log.warn("Access Denied warning: {}", accessDeniedException.getMessage());
log.warn("The Denied request has the following headers:");
request.getHeaderNames().asIterator().forEachRemaining(headerName -> {
log.warn("\tHeader: {} = {}", headerName, request.getHeader(headerName));
});
response.sendError(HttpServletResponse.SC_FORBIDDEN, "Access Denied");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package software.uncharted.terarium.hmiserver.security;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import java.io.IOException;

@Slf4j
public class LoggingAuthenticationEntryPoint implements AuthenticationEntryPoint {
@Override
public void commence(final HttpServletRequest request, final HttpServletResponse response, final AuthenticationException authException) throws IOException, ServletException {
log.warn("Unauthorized warning: {}", authException.getMessage());
log.warn("The Unauthorized request has the following headers:");
request.getHeaderNames().asIterator().forEachRemaining(headerName -> {
log.warn("\tHeader: {} = {}", headerName, request.getHeader(headerName));
});
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package software.uncharted.terarium.hmiserver.security;

import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -10,12 +11,12 @@
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.session.SessionRegistryImpl;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.preauth.AbstractPreAuthenticatedProcessingFilter;
import org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy;
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy;

import lombok.RequiredArgsConstructor;
import software.uncharted.terarium.hmiserver.filters.ServiceRequestFilter;

@Configuration
Expand All @@ -34,13 +35,23 @@ public class SecurityConfig {
private final UnauthenticatedUrlRequestMatcher unauthenticatedUrlRequestMatcher;
private final ApplicationContext applicationContext;

@Bean
public AccessDeniedHandler accessDeniedHandler(){
return new LoggingAccessDeniedHandler();
}

@Bean
public AuthenticationEntryPoint authenticationEntryPoint(){
return new LoggingAuthenticationEntryPoint();
}

@Bean
protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
}

@Bean
public SecurityFilterChain initialSecurityFilterChain(HttpSecurity http) throws Exception {
public SecurityFilterChain initialSecurityFilterChain(final HttpSecurity http) throws Exception {
http.authorizeHttpRequests((authorize) -> {
authorize
.requestMatchers(swaggerRequestMatcher).permitAll()
Expand All @@ -56,7 +67,10 @@ public SecurityFilterChain initialSecurityFilterChain(HttpSecurity http) throws
// authentication, we do not need to worry about CSRF.
http.sessionManagement(httpSecuritySessionManagementConfigurer -> httpSecuritySessionManagementConfigurer
.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.csrf(AbstractHttpConfigurer::disable);
.csrf(AbstractHttpConfigurer::disable)
.exceptionHandling()
.accessDeniedHandler(accessDeniedHandler())
.authenticationEntryPoint(authenticationEntryPoint());

return http.build();
}
Expand Down
Loading