Refactored Logged package

This commit is contained in:
Rohan Sircar 2020-05-30 14:53:35 +05:30
parent ddd4872b85
commit 26f898c37c
4 changed files with 13 additions and 50 deletions

View File

@ -1,7 +1,7 @@
package org.ros.chatto; package org.ros.chatto;
import org.ros.chatto.logged.MyLogoutSuccessHandler; import org.ros.chatto.logged.UserSessionLoggingLoginSuccessHandler;
import org.ros.chatto.logged.MySimpleUrlAuthenticationSuccessHandler; import org.ros.chatto.logged.UserSessionLoggingLogoutSuccessHandler;
import org.ros.chatto.security.CustomBasicAuthenticationFilter; import org.ros.chatto.security.CustomBasicAuthenticationFilter;
import org.ros.chatto.security.MyUserDetailsService; import org.ros.chatto.security.MyUserDetailsService;
import org.ros.chatto.security.TokenAuthenticationFilter; import org.ros.chatto.security.TokenAuthenticationFilter;
@ -18,15 +18,12 @@ import org.springframework.security.config.annotation.web.configuration.WebSecur
import org.springframework.security.config.http.SessionCreationPolicy; import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
@Configuration @Configuration
@EnableWebSecurity @EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter { public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private AuthenticationSuccessHandler authenticationSuccessHandler;
@Autowired @Autowired
private MyUserDetailsService myUserDetailsService; private MyUserDetailsService myUserDetailsService;
@Autowired @Autowired
@ -117,10 +114,10 @@ public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Order(2) @Order(2)
public static class FormWebSecurity extends WebSecurityConfigurerAdapter { public static class FormWebSecurity extends WebSecurityConfigurerAdapter {
@Autowired @Autowired
private MySimpleUrlAuthenticationSuccessHandler mySimpleUrlAuthenticationSuccessHandler; private UserSessionLoggingLoginSuccessHandler loginSuccessHandler;
@Autowired @Autowired
private MyLogoutSuccessHandler myLogoutSuccessHandler; private UserSessionLoggingLogoutSuccessHandler logoutSuccessHandler;
@Override @Override
protected void configure(HttpSecurity httpSecurity) throws Exception { protected void configure(HttpSecurity httpSecurity) throws Exception {
@ -142,8 +139,8 @@ public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
.and() .and()
.formLogin().loginPage("/login").permitAll().loginProcessingUrl("/perform_login") .formLogin().loginPage("/login").permitAll().loginProcessingUrl("/perform_login")
.successHandler(mySimpleUrlAuthenticationSuccessHandler).and().logout() .successHandler(loginSuccessHandler).and().logout()
.logoutSuccessHandler(myLogoutSuccessHandler) .logoutSuccessHandler(logoutSuccessHandler)
// .failureUrl("/?login_error") // .failureUrl("/?login_error")
// .and() // .and()
// .logout().invalidateHttpSession(true) // .logout().invalidateHttpSession(true)
@ -179,38 +176,4 @@ public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
// auth.eraseCredentials(false); // auth.eraseCredentials(false);
// } // }
// @Override
// protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// auth.inMemoryAuthentication()
// .withUser("user")
// .password("{noop}user")
// .roles("USER")
// .and()
// .withUser("admin")
// .password("{noop}admin")
// .roles("ADMIN");
//// auth.userDetailsService(myUserDetailsService);
//
// }
// @Bean
// @Override
// public UserDetailsService userDetailsService(String usern) {
//// UserDetails user =
//// User.withDefaultPasswordEncoder()
//// .username("user")
//// .password("password")
//// .roles("USER")
//// .build();
////
//// return new InMemoryUserDetailsManager(user);
// myUserDetailsService.loadUserByUsername(username)
//
// }
// @Override
// protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// auth.userDetailsService(myUserDetailsService);
// }
// auth.userDetailsService(myUserDetailsService);
// }
} }

View File

@ -13,12 +13,12 @@ import lombok.extern.slf4j.Slf4j;
@Getter @Getter
@Setter @Setter
@Slf4j @Slf4j
public class LoggedUser implements HttpSessionBindingListener { public class UserLoggingSessionListener implements HttpSessionBindingListener {
private final String username; private final String username;
private final UserService userService; private final UserService userService;
public LoggedUser(String username) { public UserLoggingSessionListener(String username) {
this.username = username; this.username = username;
userService = BeanUtil.getBean(UserService.class); userService = BeanUtil.getBean(UserService.class);
} }
@ -26,7 +26,7 @@ public class LoggedUser implements HttpSessionBindingListener {
@Override @Override
public void valueBound(HttpSessionBindingEvent event) { public void valueBound(HttpSessionBindingEvent event) {
LoggedUser user = (LoggedUser) event.getValue(); UserLoggingSessionListener user = (UserLoggingSessionListener) event.getValue();
log.debug("Incrementing session count for user {}", user.getUsername()); log.debug("Incrementing session count for user {}", user.getUsername());
@ -38,7 +38,7 @@ public class LoggedUser implements HttpSessionBindingListener {
@Override @Override
public void valueUnbound(HttpSessionBindingEvent event) { public void valueUnbound(HttpSessionBindingEvent event) {
LoggedUser user = (LoggedUser) event.getValue(); UserLoggingSessionListener user = (UserLoggingSessionListener) event.getValue();
log.debug("Decrementing session count for user {}", user.getUsername()); log.debug("Decrementing session count for user {}", user.getUsername());

View File

@ -11,7 +11,7 @@ import org.springframework.security.web.authentication.AuthenticationSuccessHand
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Component("myAuthenticationSuccessHandler") @Component("myAuthenticationSuccessHandler")
public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSuccessHandler { public class UserSessionLoggingLoginSuccessHandler implements AuthenticationSuccessHandler {
@Override @Override
public void onAuthenticationSuccess(HttpServletRequest request, public void onAuthenticationSuccess(HttpServletRequest request,
@ -19,7 +19,7 @@ public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSu
throws IOException { throws IOException {
HttpSession session = request.getSession(false); HttpSession session = request.getSession(false);
if (session != null) { if (session != null) {
LoggedUser user = new LoggedUser(authentication.getName()); UserLoggingSessionListener user = new UserLoggingSessionListener(authentication.getName());
session.setAttribute("user", user); session.setAttribute("user", user);
} }
response.sendRedirect("/chat"); response.sendRedirect("/chat");

View File

@ -12,7 +12,7 @@ import org.springframework.security.web.authentication.logout.LogoutSuccessHandl
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Component("myLogoutSuccessHandler") @Component("myLogoutSuccessHandler")
public class MyLogoutSuccessHandler implements LogoutSuccessHandler{ public class UserSessionLoggingLogoutSuccessHandler implements LogoutSuccessHandler{
@Override @Override
public void onLogoutSuccess(HttpServletRequest request, public void onLogoutSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication) HttpServletResponse response, Authentication authentication)