-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSecurityConfig.java
More file actions
209 lines (165 loc) · 8.67 KB
/
SecurityConfig.java
File metadata and controls
209 lines (165 loc) · 8.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package kr.seok;
import org.springframework.beans.factory.annotation.Autowired;
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;
import org.springframework.security.core.userdetails.UserDetailsService;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private final UserDetailsService userDetailsService;
@Autowired
public SecurityConfig(UserDetailsService userDetailsService) {
this.userDetailsService = userDetailsService;
}
/* 인가 API 설정 테스트를 위한 InMemory 계정 등록 */
// @Override
// protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// auth.inMemoryAuthentication().withUser("user").password("{noop}1234").roles("USER");
// auth.inMemoryAuthentication().withUser("sys").password("{noop}1234").roles("SYS");
// auth.inMemoryAuthentication().withUser("admin").password("{noop}1234").roles("ADMIN");
// }
@Override
protected void configure(HttpSecurity http) throws Exception {
// 인가 정책
http
/* 모든 경로에 대해서 권한 요청 설정 */
.authorizeRequests()
.antMatchers("/users").hasRole("USER")
// /* custom 한 /login resource 접근을 허용하도록 하기 위한 설정 */
// .antMatchers("/login").permitAll()
//
// /* /user 경로의 request가 들어오는 경우 인가 처리를 통해 USER role을 가진 사용자에 대해서 resource를 제공하겠다는 설정 */
// .antMatchers("/user").hasRole("USER")
// .antMatchers("/admin/pay").hasRole("ADMIN")
// .antMatchers("/admin/**").access("hasRole('ADMIN') or hasRole('SYS')")
.anyRequest()
// .authenticated()
.permitAll()
/* 3.1.1. 필터 초기화 및 다중 보안 설정 용 */
// .antMatcher("/admin/**")
// .authorizeRequests()
// .anyRequest().authenticated()
// .and()
// .httpBasic()
;
// 인증 정책
http
.formLogin()
// .loginPage("/loginPage")
// .defaultSuccessUrl("/")
// .failureUrl("/login")
// .usernameParameter("userId")
// .passwordParameter("passwd")
// .loginProcessingUrl("/login_proc")
// .successHandler(new AuthenticationSuccessHandler() {
// @Override
// public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
// System.out.println("authentication : " + authentication.getName());
// response.sendRedirect("/");
// }
// })
// .failureHandler(new AuthenticationFailureHandler() {
// @Override
// public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
// System.out.println("exception" + exception.getMessage());
// response.sendRedirect("/login");
// }
// })
// .permitAll()
// http
// .logout()
// /* GET, POST 가능 */
// .logoutUrl("/logout")
// .logoutSuccessUrl("/login")
// .addLogoutHandler(new LogoutHandler() {
// @Override
// public void logout(
// HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
// HttpSession session = request.getSession();
// session.invalidate();
// }
// })
// .logoutSuccessHandler(new LogoutSuccessHandler() {
// @Override
// public void onLogoutSuccess(
// HttpServletRequest request, HttpServletResponse response, Authentication authentication
// ) throws IOException, ServletException {
// response.sendRedirect("/login");
// }
// })
// /* 쿠키명 */
// .deleteCookies("remember-me")
// /* 캐시 필터 구현 시 formLogin 에 추가되는 handler */
// .successHandler(new AuthenticationSuccessHandler() {
// @Override
// public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
// RequestCache requestCache = new HttpSessionRequestCache();
// SavedRequest savedRequest = requestCache.getRequest(request, response);
// String redirectUrl = savedRequest.getRedirectUrl();
// response.sendRedirect(redirectUrl);
// }
// })
;
// http
// .rememberMe()
// .rememberMeParameter("remember") // 기본 파라미터 명 -> "remember-me"
// .tokenValiditySeconds(3600) // 만료시간 default 14일
// .alwaysRemember(true) // remember me 기능이 활성화되지 않아도 항상 실행
// /* user 계정 확인 메서드 */
// .userDetailsService(userDetailsService)
// ;
/* 익명 처리 */
// http
// .anonymous()
// ;
// /* 세션 관리*/
// http
// .sessionManagement()
// .maximumSessions(1)
// .maxSessionsPreventsLogin(true)
// .maxSessionsPreventsLogin(false)
// .expiredUrl("/login")
// /* 위 API 와 함께 사용할 수 없음 */
// .sessionManagement()
// .invalidSessionUrl("/login")
/* 사용자의 쿠키를 공격자의 쿠키로 인증처리 한 뒤 공격자가 해당 쿠키로 인증하는 세션 고정 공격 */
// http
/* 세션 고정 보호*/
// .sessionManagement()
// .sessionFixation()
/* 세센 고정 보호를 사용하지 않는 경우 위와 같은 문제가 발생할 수 있음 */
// .none()
// .migrateSession()
// .newSession()
/* 인증 처리 시 기존 세션 내용을 새로운 인증 세션으로 변경하는 방법 */
// .changeSessionId()
// .and()
// .sessionManagement()
/* 스프링 시큐리티가 항상 세션을 생성하는 정책 */
// .sessionCreationPolicy(SessionCreationPolicy.ALWAYS)
/* 스프링 시큐리티가 생성하지 않지만 이미 존재하면 사용 */
// .sessionCreationPolicy(SessionCreationPolicy.NEVER)
/* 스프링 시큐리티가 생성하지 않고 존재해도 사용하지 않음 */
// .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
/* 스프링 시큐리티가 필요 시 생성(기본값) */
// .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
// ;
// http
// .exceptionHandling()
// .authenticationEntryPoint(new AuthenticationEntryPoint() {
// @Override
// public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
// response.sendRedirect("/login");
// }
// })
// .accessDeniedHandler(new AccessDeniedHandler() {
// @Override
// public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
// response.sendRedirect("/denied");
// }
// })
// ;
}
}