spring security에서 제공하는 기본 form을 사용할 수도 있지만 본인은 직접 만든 form을 활용해서 로그인 작업(인증)을 진행할 예정
package com.spring.security.practice.springsecuritypractice.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
/**
* 스프링 시큐리티와 관련해서 환경 설정을 진행하는 클래스입니다.
*/
@Configuration
public class SecurityConfiguration {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.csrf().disable();
http.cors().disable();
http.formLogin()
.loginPage("/members/login");
// 관리자, 로그인할 때 볼수 있는 마이페이지 제외하고 모든 요청은 권한 없이 접속 가능
http.authorizeHttpRequests()
.anyRequest().permitAll()
.antMatchers("/myPage/**", "/manager/**").authenticated();
http.addFilterAt(A, B.class)
return http.build();
}
/**
* 비밀번호 평문 저장을 방지하기 위한 엔코더 빈등록
* */
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder(){
return new BCryptPasswordEncoder();
}
}
http.formLogin().loginPage("/members/login");
http.addFilterAt(A, B.class);
AbstractAuthenticationProcessingFilter
를 상속받아 사용하는 것이 바람직하다고 합니다.UsernamePasswordAuthenticationFilter
를 사용합니다. package com.spring.security.practice.springsecuritypractice.config.filter;
import com.spring.security.practice.springsecuritypractice.member.exception.InvalidLoginRequestException;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.authentication.AuthenticationFilter;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import javax.servlet.Filter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;
public class CustomAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
private static final AntPathRequestMatcher DEFAULT_ANT_PATH_REQUEST_MATCHER = new AntPathRequestMatcher("/members/login", "POST");
private boolean postOnly = true;
public CustomAuthenticationFilter(String defaultFilterProcessesUrl) {
super(DEFAULT_ANT_PATH_REQUEST_MATCHER);
}
@Override
public Authentication attemptAuthentication(HttpServletRequest request,
HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
String loginId = request.getParameter("loginId");
String password = request.getParameter("password");
if (!this.postOnly && !request.getMethod().equals("POST")) {
throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
}
else if (Objects.isNull(loginId) || Objects.isNull(password)){
throw new InvalidLoginRequestException();
}
Authentication authenticationToken = UsernamePasswordAuthenticationToken.unauthenticated(loginId, password)
return this.getAuthenticationManager().authenticate(authenticationToken);
}
}
In a traditional web application, logging in usually requires sending a username and password to the server for authentication. While these elements could theoretically be URL parameters in a GET request, it is obviously much better to encapsulate them into a POST request.
📌출처: https://www.baeldung.com/logout-get-vs-post
- post 요청으로 캡슐화를 해서 노출에 민감한 정보를 보호하는 편이 낫다고 한다.
- Get 요청의 경우 보안에 취약 (물론 Post 요청도)
- 가장 나은 선택은 SSL(Https) 보안 프로토콜을 사용하는 것이라고 한다.
구현한 코드 여기에 올릴 예정 . .
해당 작업들은 모두 아래처럼 진행 가능하다.
AuthenticationFilter-> setAuthenticationManager, setFailerHandler ...
AuthenticationManager -> setAuthenticationProvider 이런 흐름으로 custom한 Filter에 설정이 가능하다.