Browse Source

Refactored Logged package

master
Rohan Sircar 4 years ago
parent
commit
26f898c37c
  1. 49
      src/main/java/org/ros/chatto/WebSecurityConfiguration.java
  2. 8
      src/main/java/org/ros/chatto/logged/UserLoggingSessionListener.java
  3. 4
      src/main/java/org/ros/chatto/logged/UserSessionLoggingLoginSuccessHandler.java
  4. 2
      src/main/java/org/ros/chatto/logged/UserSessionLoggingLogoutSuccessHandler.java

49
src/main/java/org/ros/chatto/WebSecurityConfiguration.java

@ -1,7 +1,7 @@
package org.ros.chatto;
import org.ros.chatto.logged.MyLogoutSuccessHandler;
import org.ros.chatto.logged.MySimpleUrlAuthenticationSuccessHandler;
import org.ros.chatto.logged.UserSessionLoggingLoginSuccessHandler;
import org.ros.chatto.logged.UserSessionLoggingLogoutSuccessHandler;
import org.ros.chatto.security.CustomBasicAuthenticationFilter;
import org.ros.chatto.security.MyUserDetailsService;
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.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
@Configuration
@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private AuthenticationSuccessHandler authenticationSuccessHandler;
@Autowired
private MyUserDetailsService myUserDetailsService;
@Autowired
@ -117,10 +114,10 @@ public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Order(2)
public static class FormWebSecurity extends WebSecurityConfigurerAdapter {
@Autowired
private MySimpleUrlAuthenticationSuccessHandler mySimpleUrlAuthenticationSuccessHandler;
private UserSessionLoggingLoginSuccessHandler loginSuccessHandler;
@Autowired
private MyLogoutSuccessHandler myLogoutSuccessHandler;
private UserSessionLoggingLogoutSuccessHandler logoutSuccessHandler;
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
@ -142,8 +139,8 @@ public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
.and()
.formLogin().loginPage("/login").permitAll().loginProcessingUrl("/perform_login")
.successHandler(mySimpleUrlAuthenticationSuccessHandler).and().logout()
.logoutSuccessHandler(myLogoutSuccessHandler)
.successHandler(loginSuccessHandler).and().logout()
.logoutSuccessHandler(logoutSuccessHandler)
// .failureUrl("/?login_error")
// .and()
// .logout().invalidateHttpSession(true)
@ -179,38 +176,4 @@ public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
// 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);
// }
}

8
src/main/java/org/ros/chatto/logged/LoggedUser.java → src/main/java/org/ros/chatto/logged/UserLoggingSessionListener.java

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

4
src/main/java/org/ros/chatto/logged/MySimpleUrlAuthenticationSuccessHandler.java → src/main/java/org/ros/chatto/logged/UserSessionLoggingLoginSuccessHandler.java

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

2
src/main/java/org/ros/chatto/logged/MyLogoutSuccessHandler.java → src/main/java/org/ros/chatto/logged/UserSessionLoggingLogoutSuccessHandler.java

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