Ynns

페이지 로그인 시 접속한 브라우저 정보 log 저장 본문

JAVA/Spring

페이지 로그인 시 접속한 브라우저 정보 log 저장

yunassnn 2020. 4. 1. 15:11

** header 에서 브라우저 정보 가져오기

// SecurityConfig.java

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
		Authentication authentication) throws IOException, ServletException {
	// 로그인 시 로그인 날짜 업데이트 & 접속 브라우저 기록
	User user = (User) ((UserDetailImpl) authentication.getPrincipal()).getUser();
	String username 	= user.getID();
	String header 		= request.getHeader("User-Agent");
	String browser 		= null;
       
		if (header.contains("MSIE")) {
			browser = "MSIE";
		} else if (header.contains("Trident")) {
			browser = "MSIE11";
		} else if (header.contains("Chrome")) {
			browser = "Chrome";
		} else if (header.contains("Opera")) {
			browser = "Opera";
		} else if (header.contains("Firefox")) {
			browser = "Firefox";
		} else if (header.contains("Safari")) {
			browser = "Safari";
		}
			System.out.println("browser = " + header);
			userDetailsService.updateLoginDate(username);
			userDetailsService.updateLoginBrowser(browser, username);
	}
        

 

//UserDetailServiceImpl.java

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
	
	@Inject
	private UserMapper userMapper;

	@Inject
	private LoginPolicyService loginPolicy;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		User user = userMapper.findByUsername(username);
		if (null == user) {
			throw new UsernameNotFoundException("Could not find the user " + username);
		}
		
		return new UserDetailImpl(user, this.loginPolicy);
	}
	
	public boolean updateLoginDate(String username) {
		return userMapper.updateLoginDate(username);
	}

	public boolean updateLoginBrowser(String browser, String username) {
		return userMapper.updateLoginBrowser(browser, username);
	}

}

 

// Usermapper.java
...

	@Update("UPDATE " + DEFAULT_TABLE + " SET loginDate = getDate() WHERE ID = #{username}")
	public boolean updateLoginDate(String username);

	@Update("UPDATE " + DEFAULT_TABLE + " SET Browser = #{browser} WHERE ID = #{username}")
	public boolean updateLoginBrowser(@Param("browser") String browser, @Param("username") String username);
...
Comments