0

제 설정에서 시스템에 Http 요청을 보내는 업스트림 시스템이 있습니다. 이러한 HTTP 요청은 헤더에 basicAuth 토큰을 포함합니다.스프링 부트를 사용하는 BasicAuth

나는 외부 톰캣 인 Spring-boot &을 사용하고 있습니다.

사용자 이름/비밀번호가 올바른지 확인한 다음 정상적인 흐름을 따르는 지 확인하려면 응용 프로그램을 구성하고 어떻게 그렇지 않으면 로그에 예외를 인쇄합니까?

내 애플리케이션에는 UI가 없으므로 로그인 페이지/오류 페이지를 표시하고 싶지 않습니다. 내가 here처럼 발견 한 예는 UI를 기반으로합니다. 이는 제 요구 사항이 아닙니다.

또한 example처럼 tomcat을 구성해야하는 솔루션 인 경우 Springboot를 사용하고 있기 때문에 web.xml없이 어떻게 할 수 있습니까?

+0

앱에 나머지 API와 같은 스프링 코어 앱 또는 마이크로 서비스 제공 업체 만 제공합니까? – webDev

+0

@SSingh, 바로 지금 스프링 코어 애플리케이션 – reiley

+0

Just Tried It ™을 사용해 보셨나요? IIRC, Boot는 자동으로 오류 조건을 컨텐트 협상합니다. 기본 페이지의 "로그인 페이지"는 401을 반환하는 것을 의미합니다. – chrylis

답변

1

Tomcat 기본 인증을 사용하는 경우 응용 프로그램은 Tomcat 웹 컨테이너에 연결됩니다.

귀하의 앱이 스프링 부트 기반이므로 스프링 보안을 사용하고 기본 인증을 사용할 수 있다고 생각합니다.

다음은 post입니다. 여기서는 Spring Security를 ​​사용하여 보안을 설정하는 방법을 보여줍니다.

+0

그래서이 블로그에 언급 된 구현은 weblogic과 같은 다른 웹 서버뿐만 아니라 tomcat에서도 변경없이 작동합니다. 내가 맞습니까? – reiley

0

OAUTH2 서버 구성

 import org.springframework.beans.factory.annotation.Autowired; 
     import org.springframework.context.annotation.Configuration; 
     import org.springframework.security.authentication.AuthenticationManager; 
     import org.springframework.security.config.annotation.web.builders.HttpSecurity; 
     import org.springframework.security.config.http.SessionCreationPolicy; 
     import org.springframework.security.core.userdetails.UserDetailsService; 
     import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer; 
     import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter; 
     import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer; 
     import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer; 
     import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter; 
     import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer; 
     import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer; 
     import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 

    public class AuthserverApplication extends WebMvcConfigurerAdapter { 
       @Configuration 
       @EnableResourceServer 
       protected static class ResourceServer extends ResourceServerConfigurerAdapter { 
        @Override 
        public void configure(HttpSecurity http) throws Exception { 

         http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED) 
         .and() 
           .requestMatchers().antMatchers("/user/**","/api/v1/user") 
         .and() 
          .authorizeRequests() 
           .antMatchers("/user/**").authenticated() 
           .antMatchers("/api/v1/user").permitAll(); 


        } 

        @Override 
        public void configure(ResourceServerSecurityConfigurer resources) throws Exception { 
         resources.resourceId("sparklr").stateless(false); 
        } 
       } 

       @Configuration 
       @EnableAuthorizationServer 
       protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter { 
        @Autowired 
        private AuthenticationManager authenticationManager; 
        @Autowired 
        private UserDetailsService userDetailsService; 

        @Override 
        public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { 
         endpoints.authenticationManager(authenticationManager).userDetailsService(userDetailsService); 
        } 

        @Override 
        public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
         clients.inMemory().withClient("act_client").authorizedGrantTypes("password", "refresh_token").scopes("read", 
           "write", "trust"); 
        } 
       } 
      } 

UserDetailsService의 구현

import org.springframework.security.core.userdetails.UserDetailsService; 
import org.springframework.security.core.userdetails.UsernameNotFoundException; 
import org.springframework.stereotype.Service; 

import com.flasher.entity.AuthorityM; 
import com.flasher.entity.User; 
import com.flasher.repository.UserRepository; 
import java.util.HashSet; 
import java.util.Set; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.security.core.GrantedAuthority; 
import org.springframework.security.core.authority.SimpleGrantedAuthority; 
import org.springframework.security.core.userdetails.UserDetails; 

@Service 
public class UserDetailsInfo implements UserDetailsService { 

    @Autowired 
    UserRepository userRepository; 

    @Override 
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException { 
     User user = userRepository.findByUsername(userName); 
     Set<AuthorityM> authorityMs = user.getAuthorityMs(); 
     Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>(); 
     authorityMs.stream().forEach(authorityM -> { 
      authorities.add(new SimpleGrantedAuthority(authorityM.getRole())); 
     }); 
     return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), 
       authorities); 

    } 

} 

초기화하고 "org.springframework.security.core를 반환하는"org.springframework.security.core.userdetails.UserDetailsService "를 구현합니다. userdetails.User "인스턴스가 OAUTH 서버 인증을 수행합니다