Spring boot security 구현+내부작동원리
Spring boot를 이용해서 Security를 구현하는 중에, 여러블로그를 참조했으나, 기대와는 달리 계속 에러가 터져서
직접 구현하게 되었다.
https://github.com/needanyhelp/Springboot_Security_with_Authentication_and_Autorization/tree/master
needanyhelp/Springboot_Security_with_Authentication_and_Autorization
Spring Security. Contribute to needanyhelp/Springboot_Security_with_Authentication_and_Autorization development by creating an account on GitHub.
github.com
구현한 소스코드다.
Security 기본 원리 파악하기 (이론편)
특징 Spring Security는 보안에 중점을두어, 인증 및 인가등 여러 옵션을 제공하는 프레임워크이다. filter 기반으로 동작하기 때문에 spring MVC 와 분리되어 관리 및 동작한다. 설정파일은 Java, Xml 중 택
velog.io
동작원리를 참조한 사이트다.
주의할 사항들만 정리하겠다.
내가 구현한 코드는 사실 @Service, @Dao를 제외하자면 Spring Security와 관련된 코드는
public class SecurityConfig extends WebSecurityConfigurerAdapter
public UserDetails loadUserByUsername(String id) throws UsernameNotFoundException
위 두 클래스가 핵심이었다.
Spring boot가 DB에 연결되었다면, loadUserByUsername 클래스는 html Form으로부터 username, password를 입력받는다. 이때,
<form th:action="@{/login}" method="post">
<input type="text" name="username" placeholder="Enter your ID">
<input type="password" name="password" placeholder="Enter your PW">
<button type="submit" value="submit">Login</button>
</form>
여기서, html form의 name을 username으로 해주지 않으면 에러가 터진다!
loadUserByUserName함수는 입력받은 값을 이용해서 DB에서 조회해서 이용자 정보를 가져오고 UserDetails 타입으로
리턴하고, 이를 화면에서 입력한 정보와 비교해서 일치하면 Authentication 참조를 리턴하고 인증을 완료하고
다음 페이지로 넘어간다.