SecurityConfig
2024. 4. 11. 16:56ㆍ개발일지
package com.example.totalproject.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.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;
//보안인증 설정 클래스
//기존 프로그램을 사용자가 임의로 변경
//클래스나 메소드명이나 이미 정의된 것으로만 사용
@EnableWebSecurity
public class SecurityConfig {
//비밀번호 생성 방식(암호화)
@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
//가상계정관리
//username, password, 권한(USER, MANAGER, ADMIN, MEMBER)
/*데이터베이스 연동시 삭제
@Bean
public InMemoryUserDetailsManager userDetailsService(){
//UserDetailsEntity
UserDetails user = User.builder()
.username("sample")
.password(passwordEncoder().encode("1234"))
.roles("MANAGER")
.build();
return new InMemoryUserDetailsManager(user);
}*/
//모든 사용자-.permitAll(), 해당 사용자-.hasRole(), 해당 사용자들-.hasAnyRole()
//로그인 인증-.authenticated()
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
//권한범위(인증후 응답)
//requestMatchers("매핑명").권한부여
//Controller에 접근 권한
http.authorizeHttpRequests((auth)->{
auth.requestMatchers("/").permitAll();
//auth.requestMatchers("/login").permitAll();
auth.requestMatchers("/board/list").permitAll();
auth.requestMatchers("/board/read").permitAll();
auth.requestMatchers("/movie/list").permitAll();
auth.requestMatchers("/movie/read").permitAll();
auth.requestMatchers("/member/save").permitAll();
auth.requestMatchers("/member/logout").permitAll();
auth.requestMatchers("/member/list").hasRole("ADMIN");
auth.requestMatchers("/member/modify").hasRole("ADMIN");
auth.requestMatchers("/member/remove").hasRole("ADMIN");
auth.requestMatchers("/movie/save").hasRole("ADMIN");
auth.requestMatchers("/movie/modify").hasRole("ADMIN");
auth.requestMatchers("/movie/remove").hasRole("ADMIN");
auth.requestMatchers("/board/save").hasAnyRole("USER", "ADMIN");
auth.requestMatchers("/board/modify").hasAnyRole("USER", "ADMIN");
auth.requestMatchers("/board/modify").hasRole("ADMIN");
auth.requestMatchers("/css/**", "/js/**", "/img/**", "/images/**").permitAll();
});
//로그인설정(기본 /login)
//loginPage("매핑명") - 사용자 로그인 페이지
//defaultSuccessURl("매핑명") - 로그인 성공시 이동할 페이지
//failureUrl("매핑명") - 로그인 실패시 이동할 페이지
//usernameParameter(사용자가 설정한 아이디변수명)
//passwordParameter(사용자가 설정한 비밀번호변수명)
//로그인 페이지는 모든 사용자가 접근 가능하고, 로그인 성공시 /user페이지로 이동
http.formLogin(login-> login
.loginPage("/login")
.usernameParameter("loginid")
.defaultSuccessUrl("/", true)
.permitAll()
);
//로그아웃설정(기본 /logout)
http.logout(logout->logout
.logoutSuccessUrl("/")
);
//csrf 사용X
http.csrf(AbstractHttpConfigurer::disable);
return http.build();
}
}
'개발일지' 카테고리의 다른 글
Spring Security - 회원가입 강화 (0) | 2024.04.16 |
---|---|
PaginationUtil (0) | 2024.04.11 |
LoginService (0) | 2024.04.11 |
FileService (0) | 2024.04.11 |
movice-service (0) | 2024.04.11 |