본문 바로가기

STUDY/Spring

Spring | 스프링 시큐리티(Spring Security) (4) MyBatis이용 로그인

이전 글에서 해놓은대로, DB에 변환된 비밀번호가 저장되어있어야 함.

 

 

1. CustomUserDetails와 CustomUserDetailsService클래스 생성 및 작성

 

 

 

CustomUserDetails는 UserDetails 인터페이스를 상속받음. 기존의 UserDto라고 생각하면 쉬움.

* getAuthorities : 회원의 auth(role)정보 getter

 

package com.gaemi.wiw.security;

import java.util.ArrayList;
import java.util.Collection;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

@SuppressWarnings("serial")
public class CustomUserDetails implements UserDetails {
	
	private String id;
	private String password;
	private String name;
	private String auth;
	private int enabled;
	
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		ArrayList<GrantedAuthority> authList = new ArrayList<GrantedAuthority>();
		authList.add(new SimpleGrantedAuthority(auth));
		return authList;
	}

	@Override
	public String getPassword() {
		return password;
	}

	@Override
	public String getUsername() {
		return id;
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return enabled==1?true:false;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

 

 

CustomUserDetailsService역시 UserService라고 생각하면 쉬움.

 

package com.gaemi.wiw.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.gaemi.wiw.dao.UserDao;

public class CustomUserDetailsService implements UserDetailsService {

	@Autowired
	private UserDao userDao;
	
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		CustomUserDetails users = userDao.getUserById(username);
		if(users == null) {
			 throw new UsernameNotFoundException("username " + username + " not found");
		}
		System.out.println("**************Found user***************");
		System.out.println("id : " + users.getUsername());
		return users;
	}

}

 

 

2. userDao및 MyBatis mapper작성

 

userDao클래스 생략.

userDaoImpl클래스에서 오버라이딩해줌! 리턴값은 CustomUserDetails임

한 회원의 값(한 개의 row)만 가져오기 때문에 selectOne

@Override
public CustomUserDetails getUserById(String id) {
  CustomUserDetails users = sqlSession.selectOne(namespace + "getUserById", id);
  return users;
}

 

mapper작성

resultType또한 CustomUserDetails가 되어야 함.

<select id="getUserById" parameterType="String" resultType="com.gaemi.wiw.security.CustomUserDetails">
	SELECT * FROM USERS WHERE ID=#{id}
</select>

 

 

 

3. sprincg-security.xml에 userService등록 및 로그인 방식 수정

 

 

class경로 인식 못할 경우에 base-package지정해주기!

 

<context:component-scan base-package="com.gaemi.wiw" />
<bean id="customUserService" class="com.gaemi.wiw.security.CustomUserDetailsService" />

 

 

user-service-ref에 위에서 등록한 bean의 id값 입력

 

<security:authentication-manager>	
  <security:authentication-provider user-service-ref="customUserService">
  	<security:password-encoder hash="bcrypt"/>
  </security:authentication-provider>
</security:authentication-manager>

 

 

 

4. 결과

 

로그인이 되어 LOGIN버튼이 LOGOUT으로 변경됨!