Java
[JAVA]Spring Security 사용 시 404에러
leeseokwoon
2022. 5. 27. 17:54
반응형
실행환경 : Spring Security 2.7.0
에러현상 : 자체 폼 로그인 구현시 loginForm.html 404 에러
HttpSecurity설정
package com.example.Securitytest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.WebSecurityConfigurer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
LoginIdPwValidator loginIdPwValidator;
// @Bean
// public ViewResolver viewResolver() {
// InternalResourceViewResolver resolver = new InternalResourceViewResolver();
// resolver.setOrder(0);
// resolver.setPrefix("/resources/templates/");
// resolver.setSuffix(".html");
//
// return resolver;
// }
// 정적자원에 대해서는 Security를 설정하지않음
@Override
public void configure(WebSecurity web) throws Exception {
// web.ignoring().requestMatchers(PathRequest.toStaticResources().atCommonLocations());
web.ignoring() .antMatchers("/resources/**")
.antMatchers("/css/**")
.antMatchers("/vendor/**")
.antMatchers("/js/**")
.antMatchers("/favicon*/**")
.antMatchers("/img/**");
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(loginIdPwValidator);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/home").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/loginForm")
.loginProcessingUrl("/loginProc")
.usernameParameter("id")
.passwordParameter("pw")
.defaultSuccessUrl("/view/dashboard", true)
.permitAll()
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logoutProc"));
}
}
1. 정적 리소스 자원에 대한 허용
- html 파일 내에서 이미지 파일 또는 CSS파일 등 정적파일 사용을 위함.
2. 테스트를 위한 /home URL 허용 및 , 외 URL 자체 로그인 폼으로 연결
Controller 설정
@Controller
public class UserController {
@GetMapping("/loginForm")
String loginForm(){
return "loginForm";
}
@GetMapping("/home")
String home(){
System.out.println("IN");
return "home";
}
}
1. URL 맵핑 진행
이슈 원인 : 템플릿 엔진 미사용시 Resource경로에 대한 핸들링 필요
ResourceHandlerRegistry (Spring Framework 5.3.20 API)
Stores registrations of resource handlers for serving static resources such as images, css files and others through Spring MVC including setting cache headers optimized for efficient loading in a web browser. Resources can be served out of locations under
docs.spring.io
@Configuration
public class MvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/templates/")
.setCacheControl(CacheControl.noCache());
}
}
- addResourceHandler("/**") - 모든 리소스 허용
- addResourceLocations("classpath:/templates/") - 리소스 위치 설정
문제점
동적페이지 구현을 위한 템플릿엔진을 사용하면 .html 정적 파일을 템플릿 엔진에서 활용하기 때문에 리소스 핸들링을
템플릿 엔진에서 설정되었으나, 미설치 시 해당 설정을 자체 설정해야함.
반응형