spring-boot-starter-security 의존성 주입
[ controller ]
package com.cos.security1.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class indexController { @GetMapping({"","/"}) public String index(){ // mustache 머스태치 // 머스태치 기본폴더 src/main/resources/ // viewResolver 설정 : templates (prefix), mustache (suffix) return "index"; // src/main/resources/templates/index.mustache } @GetMapping("/user") public @ResponseBody String user(){ return "user"; } @GetMapping("/admin") public @ResponseBody String admin(){ return "admin"; } @GetMapping("/manager") public @ResponseBody String manager(){ return "manager"; } // 스프링시큐리티가 해당주소를 낚아채버림 // SecurityConfig 파일에서 설정으로 낚아채지 않도록 ! @GetMapping("/login") public @ResponseBody String login(){ return "login"; } @GetMapping("/join") public @ResponseBody String join(){ return "join"; } @GetMapping("/joinProc") public @ResponseBody String joinProc(){ return "회원가입 완료됨!"; } }
[ config ]
package com.cos.security1.config; 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.configuration.WebSecurityConfigurerAdapter; @Configuration @EnableWebSecurity // 활성화 - 스프링 시큐리티 필터가 스프링 필터체인에 등록이 된다. public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable(); // /login으로 갈때 스프링 시큐리티가 낚아채지 않도록 http.authorizeRequests() .antMatchers("/user/**").authenticated() .antMatchers("/manager/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_MANAGER')") .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')") .anyRequest().permitAll()// 다른주소는 OK .and() .formLogin() .loginPage("/login"); // 권한이 없다면 403페이지가 뜨는게 아니라 login페이지를 뜨게만들어줌 } }
[ webMvcConfiguer ]
- 머스태치 설정
- view 주소 설정
package com.cos.security1.config; import org.springframework.boot.web.servlet.view.MustacheViewResolver; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ViewResolverRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void configureViewResolvers(ViewResolverRegistry registry) { MustacheViewResolver resolver = new MustacheViewResolver(); resolver.setCharset("UTF-8"); resolver.setContentType("text/html; charset=UTF-8"); resolver.setPrefix("classpath:/templates/"); resolver.setSuffix(".html"); // .mustache가 아닌 .html로 바꿔주기 위한 작업 registry.viewResolver(resolver); // viewResolver 등록 } }
'spring' 카테고리의 다른 글
[spring security] OAuth2.0 기본 - 구글 로그인 준비 (0) | 2022.01.17 |
---|---|
[Spring Security] 스프링 시큐리티 3. 권한부여 (0) | 2022.01.16 |
[Spring Security] 스프링 시큐리티 2. 로그인 (0) | 2022.01.16 |
[스프링부트] vs code 사용하기 메모 (0) | 2021.03.08 |
[스프링] 웹소켓 : 채팅 (0) | 2021.01.29 |
[스프링] 메일발송하기 (0) | 2021.01.25 |
[스프링] REST API / @RequestBody @ResponseBody , HttpMessageConverter (0) | 2021.01.20 |