인증이 되지 않은 사용자가 접근할 수 있는 주소를 /auth/로 수정했음.
각종 url을 맞게 수정함.
loginForm.jsp 의 버튼을 다시 form 안쪽에 집어넣고 post 메서드를 넣어줬음.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="../layout/header.jsp"%>
<div class="container">
<form action="#" method="post">
<div class="form-group">
<label for="uname">Username:</label> <input type="text" class="form-control" id="username" placeholder="아이디를 입력해주세요." name="username" required>
</div>
<div class="form-group">
<label for="pwd">Password:</label> <input type="password" class="form-control" id="password" placeholder="비밀번호를 입력해주세요." name="password" required>
</div>
<div class="form-group form-check">
<label class="form-check-label"> <input class="form-check-input" type="checkbox" name="remember" required> 로그인 상태 유지
</label>
</div>
<button id="btn-login" class="btn btn-primary">로그인</button>
</form>
</div>
<%@ include file="../layout/footer.jsp"%>
user.js에서 btn-login 부분을 삭제함.
config 패키지를 생성한 후 SecurityConfig.java를 생성함.
package com.pure.blog.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
//이 세가지는 세트 상품이라고 생각하면 편함.
@Configuration // 빈 등록
@EnableWebSecurity // 시큐리티 필터 추가 == 시큐리티 설정을 이 클래스에서 하겠다.
@EnableGlobalMethodSecurity(prePostEnabled = true) //특정 주소로 접근하면 권한 및 인증을 미리 체크함.
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/auth/**")
.permitAll()
.anyRequest()
.authenticated()
.and()
.formLogin()
.loginPage("/auth/loginForm");
}
}
어노테이션 3개는 패키지 상품이라고 생각하면 되고 http 메서드들의 위의 것은 /auth/로 시작하는 주소에 대해서 접근을 모두 허용하라는 것. 아래 것은 로그인 페이지를 내가 만든 것으로 바꾼 것.
'취업 준비 > Spring boot' 카테고리의 다른 글
28. XSS와 CSRF (0) | 2022.01.26 |
---|---|
27. 비밀번호 해쉬 후 회원가입하기 (0) | 2022.01.26 |
25. 스프링 시큐리티 사용해보기 (0) | 2022.01.26 |
24. 스프링 시큐리티 시작 전 컨텍스트 패스 모두 수정 (0) | 2022.01.26 |
23. 전통적인 방식의 로그인 방법 (0) | 2022.01.26 |