Spring Security 에서 제공하는 것이 아닌 직접 만든 LoginPage로 옮기기위한 작업을 진행한다.
로그인 페이지를 생성하고, url을 맵핑하고, 컨트롤러를 만들어 넘긴다.
LoginPage 이동을 위한 컨트롤러.
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class LoginController {
@GetMapping(value = "/login")
public String login(){
return "user/login/login";
}
}
제공할 로그인 페이지 (Thymeleaf) . Bootstrap
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="layout/header::userHead"></head>
<body>
<div th:replace="layout/top::header"></div>
<div class="container text-center">
<div class="login-form d-flex justify-content-center">
<div class="col-sm-5" style="margin-top: 30px;">
<div class="panel">
<p>아이디와 비밀번호를 입력해주세요.</p>
</div>
<form th:action="@{/login_proc}" class="form-signin" method="post">
<div class="form-group">
<input type="text" name="username" class="form-control" placeholder="아이디" required="required" autofocus="autofocus">
</div>
<div class="form-group">
<input type="password" name="password" class="form-control" placeholder="비밀번호" required="required" autofocus="autofocus">
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">로그인</button>
</form>
</div>
</div>
</div>
</body>
</html>
SpringSecurity 설정 클래스
- formLogin() API 뒤로 로그인 페이지 제공을 위한 API들이 제공된다.
- loginPage() : 로그인 할 페이지 요청 경로
- defaultSuccessUrl() : 로그인 성공시 이동 경로
- loginProcessingUrl() : 폼 인증시 넘길 Action 속성
- permitAll() : 여기서는 로그인 요청에 대한 접근이 인증없이 허가.
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/","/users").permitAll()
.antMatchers("/mypage").hasRole("USER")
.antMatchers("/messages").hasRole("MANAGER")
.antMatchers("/config").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/")
.loginProcessingUrl("/login_proc")
.permitAll();
}
'springframework > 시작하자SpringSecurity' 카테고리의 다른 글
30.WebAuthenticationDetails, AuthenticationDetailsSource (0) | 2020.09.29 |
---|---|
29.Logout 화면과 보안처리 (0) | 2020.09.28 |
27.인증구현, CustomAuthenticationProvider (0) | 2020.09.27 |
26.인증 프로세스 구현, CutonUserDetailsService (0) | 2020.09.27 |
26.PasswordEncoder (0) | 2020.09.27 |