Skip to content

Commit

Permalink
SHIBUI-2578/2579
Browse files Browse the repository at this point in the history
Slight change to handling the auth event
  • Loading branch information
chasegawa committed May 31, 2023
1 parent 05e6811 commit b68408b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,18 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.authentication.AuthenticationEventPublisher;
import org.springframework.security.authentication.DefaultAuthenticationEventPublisher;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
Expand Down Expand Up @@ -293,4 +298,24 @@ public List<URL> beaconEndpointUrl(@Value("${shibui.beacon.url}") String urls) t
}
return result;
}

@Bean
public AuthenticationEventPublisher authenticationEventPublisher(ApplicationEventPublisher applicationEventPublisher, UserService userService) {
return new RecordLoginHandler(applicationEventPublisher, userService);
}

class RecordLoginHandler extends DefaultAuthenticationEventPublisher {
private UserService userService;

public RecordLoginHandler(ApplicationEventPublisher applicationEventPublisher, UserService userService) {
super(applicationEventPublisher);
this.userService = userService;
}

@Override
public void publishAuthenticationSuccess(Authentication authentication) {
super.publishAuthenticationSuccess(authentication);
userService.updateLoginRecord(authentication.getName());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
.roles("ADMIN");
}
auth.userDetailsService(adminUserService(userService)).passwordEncoder(passwordEncoder);
auth.authenticationEventPublisher(new RecordLoginHandler(userService));
}

@Override
Expand Down Expand Up @@ -169,23 +168,4 @@ public void configure(WebSecurity web) throws Exception {
}
};
}

class RecordLoginHandler implements AuthenticationEventPublisher {
private UserService userService;

public RecordLoginHandler(UserService userService) {
this.userService = userService;
}

@Override
public void publishAuthenticationSuccess(Authentication authentication) {
// do this in SimpleAuthenticationProvider in v2.0
userService.updateLoginRecord(authentication.getName());
}

@Override
public void publishAuthenticationFailure(AuthenticationException exception, Authentication authentication) {
// don't care about this
}
}
}

0 comments on commit b68408b

Please sign in to comment.