405에러가 발생합니다. 이유를 잘 모르겠습니다.
591
작성한 질문수 2
1
package com.attendance.scheduler.infra.config.security;
import lombok.RequiredArgsConstructor;
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.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
@RequiredArgsConstructor
@EnableWebSecurity
public class SecurityConfig {
public static final String[] ENDPOINTS_WHITELIST = {
"/", "/submit", "/completion",
"/class/**",
"/board/**",
"/join/**",
"/cert/**",
"/help/**",
"/comment/**",
"/css/**",
"/js/**"
};
private final CustomAuthenticationFailureHandler customAuthenticationFailureHandler;
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public SecurityFilterChain adminFilterChain(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.csrf(AbstractHttpConfigurer::disable)
.securityMatcher("/admin/**","/manage/**")
.authorizeHttpRequests(auth -> auth
.requestMatchers("/admin/**")
.hasAuthority("ADMIN")
.requestMatchers("/manage/**")
.hasAnyAuthority("ADMIN", "TEACHER")
.anyRequest().authenticated())
.formLogin(httpSecurityFormLoginConfigurer -> httpSecurityFormLoginConfigurer
.defaultSuccessUrl("/manage/class", true)
.failureHandler(customAuthenticationFailureHandler)
.loginPage("/login")
.loginProcessingUrl("/login")
)
.logout(httpSecurityFormLogoutConfigurer -> httpSecurityFormLogoutConfigurer
.logoutUrl("/logout")
.invalidateHttpSession(true)
.deleteCookies("JSESSIONID")
.logoutSuccessUrl("/"))
.sessionManagement(sessionManagement -> sessionManagement
.invalidSessionUrl("/login")
.maximumSessions(1)
.maxSessionsPreventsLogin(true)
.expiredUrl("/login"));
return httpSecurity.build();
}
}
2
package com.attendance.scheduler.infra.config.security;
import com.attendance.scheduler.admin.domain.AdminEntity;
import com.attendance.scheduler.admin.repository.AdminJpaRepository;
import com.attendance.scheduler.infra.config.security.Admin.AdminDetails;
import com.attendance.scheduler.infra.config.security.User.TeacherDetails;
import com.attendance.scheduler.teacher.domain.TeacherEntity;
import com.attendance.scheduler.teacher.repository.TeacherJpaRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
@Slf4j
@Component
@RequiredArgsConstructor
public class AccountDetailService implements UserDetailsService {
private final AdminJpaRepository adminJpaRepository;
private final TeacherJpaRepository teacherJpaRepository;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
log.info("teacherId = {}", username);
final TeacherEntity teacherEntity = teacherJpaRepository
.findByUsernameIs(username);
if(teacherEntity != null){
return new TeacherDetails(teacherEntity);
} else {
final AdminEntity adminEntity = adminJpaRepository
.findByUsernameIs(username);
if (adminEntity != null) {
log.info("adminId = {}", username);
return new AdminDetails(adminEntity);
}
}
throw new UsernameNotFoundException(username);
}
}3
<form method='post' th:action="@{/login}" th:object="${login}">
로그인을 진행하면 405에러가 계속 발생합니다. 이유를 찾고 있으나 아이디와 비밀번호를 입력해도 2번 코드의 로그에 남지 않습니다. 혹시 이유를 알려주실수 있을까요? 감사합니다.
답변 2
0
@Bean
public SecurityFilterChain adminFilterChain(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.csrf(AbstractHttpConfigurer::disable)
.securityMatcher("/admin/**","/manage/**")위 코드에서 .securityMatcher("/admin/**","/manage/**") 가 설정되어 있는데 이렇게 되면 /admin/** 와 /manage/** 경로로 접근하는 요청 이외에는 시큐리티 보안을 타지 않습니다.
그리고 현재
.formLogin(httpSecurityFormLoginConfigurer -> httpSecurityFormLoginConfigurer
.defaultSuccessUrl("/manage/class", true)
.failureHandler(customAuthenticationFailureHandler)
.loginPage("/login")
.loginProcessingUrl("/login")
.permitAll() // 추가
)폼 로그인 접근도 permitAll 이 되어 있지 않는데 이 부분도 인증받지 않는 상태에서도 접근이 가능하도록 해 주어야 합니다. 그렇지 않으면 무한 반복에 빠질 수 있습니다.
일단 위와 같이 설정하면 로그인은 되는데 403 으로 나오고 있습니다.
이것은 권한 문제인데 확인해 보시기 바랍니다.
로그아웃-logout()-2 강에서 겟방식 로그아웃 호출 후 화면이동 질문입니다.
0
28
2
단원별 소스코드
0
60
2
CustomAuthenticationProvider 추가 관련 문의
0
69
2
AOP 의존성 명칭 변경
0
65
1
빈 1개 등록 시 다른 해결 방법
0
65
1
@Bean으로 AuthenticationProvider를 등록 시 http.authenticationProvider 함수를 이용해서 추가해줘야되나요?
0
85
2
OIDC의 id token에 담긴 데이터에 대해
0
74
1
loginPage("/loginPage") 질문드립니다.
0
69
1
@EnableWebSecurity
0
147
1
트랜잭션과 롤백
0
99
1
68. 인증 이벤트 - AuthenticationEventPublisher 활용 강좌 음성 문제
0
89
2
AuthenticationManager 사용 방법
0
148
2
HttpSecurity.authorizeHttpRequests() - 2 강의 부분에 대한 질문
0
104
2
spring security 6.3에서는 HttpSecurity가 만들어지기 전 WebSecurity가 먼저 만들어지는게 맞나요??
0
190
1
init(B Builder), configure(B builder) 에 대하여 질문 드립니다.
0
106
2
메타 주석 질문
0
68
1
동시세션제어 기능에서 로그아웃하기
0
147
3
로그인 후, redirect 에서 error
0
138
3
Session 생성 타이밍에 대한 질문
0
84
2
강의 참고 내용을 개발 로그로 작성해도 될지 문의드립니다.
0
133
2
customAuthentication 관련
0
127
2
authenticationManagerBuilder 주입받은거 vs 만든 거
0
110
1
UserDetailsService()에서 UserDetail이 아닌 타입을 반환할 수 있나요?
0
101
1
9:28 패턴 3의 경우 마지막으로 설정한 것만 적용되는 것 같습니다.
0
159
2





