접근 허용 url 설정(permitAccess) 추가

master
mjkhan21 1 year ago
parent 46c4614704
commit 1ceb84b343

@ -1,5 +1,11 @@
package cokr.xit.base.boot; package cokr.xit.base.boot;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Resource; import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -24,6 +30,7 @@ import cokr.xit.base.security.authentication.web.AuthenticationExtraDetailsSourc
import cokr.xit.base.security.authentication.web.AuthenticationFailure; import cokr.xit.base.security.authentication.web.AuthenticationFailure;
import cokr.xit.base.security.authentication.web.AuthenticationSuccess; import cokr.xit.base.security.authentication.web.AuthenticationSuccess;
import cokr.xit.base.security.authentication.web.LogoutSuccess; import cokr.xit.base.security.authentication.web.LogoutSuccess;
import cokr.xit.foundation.AbstractComponent;
import cokr.xit.foundation.boot.StaticResourceConfig; import cokr.xit.foundation.boot.StaticResourceConfig;
import cokr.xit.foundation.web.ExceptionController; import cokr.xit.foundation.web.ExceptionController;
@ -32,7 +39,7 @@ import cokr.xit.foundation.web.ExceptionController;
*/ */
@Configuration @Configuration
@EnableWebSecurity @EnableWebSecurity
public class SecurityConfig { public class SecurityConfig extends AbstractComponent {
@Autowired @Autowired
private ExceptionController exceptionController; private ExceptionController exceptionController;
@Resource(name = "staticResource") @Resource(name = "staticResource")
@ -55,13 +62,9 @@ public class SecurityConfig {
*/ */
@Bean @Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
String[] permitAccess = getPermittedAccess(false);
http.authorizeHttpRequests(conf -> http.authorizeHttpRequests(conf ->
conf.antMatchers( conf.antMatchers(permitAccess).permitAll()
"/login.do",
"/logout.do",
"/error/*.do",
"/api/**/*.do"
).permitAll()
.antMatchers("/**/*.do").access(authorizationManager()) .antMatchers("/**/*.do").access(authorizationManager())
.anyRequest().authenticated() .anyRequest().authenticated()
) )
@ -94,15 +97,39 @@ public class SecurityConfig {
return http.build(); return http.build();
} }
private String[] getPermittedAccess(boolean ignoringOnly) {
String str = properties.getString("permitAccess", "");
List<String> ignoring = !str.isEmpty() ? Stream.of(str.split(",")).map(String::trim).toList() : Collections.emptyList();
if (ignoringOnly) {
return ignoring.toArray(new String[ignoring.size()]);
}
List<String> urls = Stream.of("/login.do", "/logout.do", "/error/*.do")
.collect(Collectors.toCollection(() -> new ArrayList<>()));
if (!ignoring.isEmpty())
ignoring.forEach(s -> {
s = s.trim();
if (!s.isEmpty())
urls.add(s);
});
return urls.toArray(new String[urls.size()]);
}
/**WebSecurityCustomizer .<br /> /**WebSecurityCustomizer .<br />
* url /resources/** . * url /resources/** .
* @return WebSecurityCustomizer * @return WebSecurityCustomizer
*/ */
@Bean @Bean
public WebSecurityCustomizer webSecurityCustomizer() { public WebSecurityCustomizer webSecurityCustomizer() {
return conf -> conf.ignoring() String[] urls = getPermittedAccess(true);
.antMatchers("/api/**/*.do") return conf -> {
conf.ignoring()
.antMatchers(staticResource.getURLs(null)); .antMatchers(staticResource.getURLs(null));
if (urls.length > 0)
conf.ignoring()
.antMatchers(urls);
};
} }
/**AuthenticationSuccess( ) . /**AuthenticationSuccess( ) .

Loading…
Cancel
Save