package com.urpsys.kccfbat.config; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpMethod; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter; import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer; import org.springframework.security.oauth2.provider.token.TokenStore; import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter; import org.springframework.security.oauth2.provider.token.store.JwtTokenStore; import com.google.gson.Gson; import kong.unirest.JsonNode; import kong.unirest.Unirest; import lombok.extern.slf4j.Slf4j; import com.urpsys.kccfbat.common.exception.CustomException; import com.urpsys.kccfbat.domain.CommonVO; import com.urpsys.kccfbat.domain.JWTKey; import com.urpsys.kccfbat.service.CommonService; /** * Resource 서버 기본세팅 * * @author 나혁제 * @since 2023.02.28 * @version 1.0.0 * @see * @comment : * *
 *
 * << 개정이력(Modification information) >>
 *
 *   수정일       수정자         수정내용
 * -----------  ---------    ------------------------
 * 2023.02.28  나혁제         최초작성
 *
 * 
*/ @Slf4j @Configuration public class Oauth2ResourceConfig extends ResourceServerConfigurerAdapter { @Autowired CommonService commonService; // 공통 서비스 @Override public void configure(ResourceServerSecurityConfigurer resources) throws Exception { resources.resourceId("event"); // 최소한 리소스ID 정도는 설정 필요 } @Value("${spring.security.oauth2.resourceserver.jwt.jwk-set-uri}") private String publicKeyUri; // key 정보를 받아오기 위한 인증서버 URL 선언 @Override public void configure(HttpSecurity http) throws Exception { http.authorizeRequests().mvcMatchers("/test/**" ).permitAll(); // test API http.authorizeRequests().mvcMatchers("/api/**" ).permitAll(); // test API http.authorizeRequests().mvcMatchers("/server/**" ).permitAll(); // test API http.authorizeRequests().mvcMatchers("/swagger-resources/**" ).permitAll(); // api 문서 관련 http.authorizeRequests().mvcMatchers("/swagger-ui**" ).permitAll(); // api 문서 관련 http.authorizeRequests().mvcMatchers("/swagger-ui/**" ).permitAll(); // api 문서 관련 http.authorizeRequests().mvcMatchers("/webjars/**" ).permitAll(); // api 문서 관련 http.authorizeRequests().mvcMatchers("/v2/**" ).permitAll(); // api 문서 관련 http.authorizeRequests().mvcMatchers("/swagger/**" ).permitAll(); // api 문서 관련 http.authorizeRequests().anyRequest().authenticated(); } @Bean public TokenStore tokenStore() { log.info(">>>>>>>>>> Oauth2ResourceConfig tokenStore <<<<<<<<<<"); log.info("<<<<<<<<<< Oauth2ResourceConfig tokenStore >>>>>>>>>>"); return new JwtTokenStore(jwtAccessTokenConverter()); } @Bean public JwtAccessTokenConverter jwtAccessTokenConverter() { log.info(">>>>>>>>>> Oauth2ResourceConfig jwtAccessTokenConverter <<<<<<<<<<"); try { /*** * 직접 oauth 서버를 호출하여 공개키 읽어서 jwt 디코드 키 등록 */ JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); // 보안키 획득 String strPrivKey = getPublicKeyValue(publicKeyUri); // if(strPrivKey==null || strPrivKey.equals("")) { throw new CustomException("보안키 획득 실패1"); } converter.setVerifierKey(strPrivKey); log.info("<<<<<<<<<< Oauth2ResourceConfig jwtAccessTokenConverter >>>>>>>>>>"); return converter; } catch (Exception e) { log.info("보안키 획득 실패2"); log.info("<<<<<<<<<< Oauth2ResourceConfig jwtAccessTokenConverter >>>>>>>>>>"); return new JwtAccessTokenConverter(); } } private String getPublicKeyValue(String uriKey) { log.info(">>>>>>>>>> Oauth2ResourceConfig getPublicKeyValue <<<<<<<<<<"); String strReturn = ""; JsonNode response = null; try { response = Unirest.get(uriKey) .asJson().getBody(); strReturn = response.toString(); } catch(Exception e) { return strReturn; } if(StringUtils.isEmpty(strReturn)) { strReturn =""; } else { strReturn =new Gson().fromJson(response.toString(), JWTKey.class).getValue();; } log.info("<<<<<<<<<< Oauth2ResourceConfig getPublicKeyValue : {} >>>>>>>>>>", strReturn); return strReturn; } }