programing

org.springframework 유형의 빈을 정의하는 것을 검토합니다.보안.인증이 필요합니다.AuthenticationManager'가 설정되어 있습니다.

golfzon 2023. 3. 10. 23:01
반응형

org.springframework 유형의 빈을 정의하는 것을 검토합니다.보안.인증이 필요합니다.AuthenticationManager'가 설정되어 있습니다.

저는 여기에 언급된 몇 가지 제안을 따랐지만, 제게는 효과가 없었습니다.그래서 여기에 질문을 넣는 것은

  1. 커스텀 필터에 Java Configuration을 사용하여 Authentication Manager를 삽입하는 방법
  2. 스프링에 'AuthenticationManager' 유형의 빈이 필요함

무엇이 문제이며, 어떻게 그것을 해결할 수 있는지 안내해주실 수 있나요?

오류:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field authenticationManager in com.techprimers.security.springsecurityauthserver.config.AuthorizationServerConfig required a bean of type 'org.springframework.security.authentication.AuthenticationManager' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.security.authentication.AuthenticationManager' in your configuration.

Authorization Server Config.java

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {

        security.tokenKeyAccess("permitAll()")
                .checkTokenAccess("isAuthenticated()");
    }


    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients
                .inMemory()
                .withClient("ClientId")
                .secret("secret")
                .authorizedGrantTypes("authorization_code")
                .scopes("user_info")
                .autoApprove(true);
    }


    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

        endpoints.authenticationManager(authenticationManager);
    }
}

Resource Server Config.java

@EnableResourceServer
@Configuration
public class ResourceServerConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    @Qualifier("authenticationManagerBean")
    private AuthenticationManager authenticationManager;
    @Autowired
    private UserDetailsService customUserDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.requestMatchers()
                .antMatchers("/login", "/oauth/authorize")
                .and()
                .authorizeRequests()
                .anyRequest()
                .authenticated()
                .and()
                .formLogin()
                .permitAll();
    }


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.parentAuthenticationManager(authenticationManager)
                .userDetailsService(customUserDetailsService);
    }
}

https://github.com/TechPrimers/spring-security-oauth-mysql-example,에서 가져온 코드 참조는 스프링 부트 부모 버전만 업데이트했습니다.2.0.4.RELEASE물건들이 부서지기 시작했어요

Spring Boot 2.0에서 도입된 "Breaking Changes" 중 하나인 것 같습니다.고객님의 케이스는 Spring Boot 2.0 이행가이드에 기재되어 있다고 생각합니다.

고객님의 고객명WebSecurityConfigurerAdapter덮어쓸 클래스authenticationManagerBean방법 및 주석을 붙입니다.@Bean(예:

@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
}
just add this to the AuthenticationManagerBuilder

@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
}

and in your controller where you need to use it add this :

 @Autowired
    private AuthenticationManager authenticationManager;

등록하는 것을 검토하는 것이 좋습니다.GlobalAuthenticationConfigurerAdapter를 설정하다AuthenticationManager

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration {

    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration authConfig) throws Exception {
        final List<GlobalAuthenticationConfigurerAdapter> configurers = new ArrayList<>();
        configurers.add(new GlobalAuthenticationConfigurerAdapter() {
                    @Override
                    public void configure(AuthenticationManagerBuilder auth) throws Exception {
                        // auth.doSomething()
                    }
                }
        );
        return authConfig.getAuthenticationManager();
    }

}

예를 들어, 고객님이 고객님을 등록하고 싶어한다고 가정했습니다.UserDetailsService(즉,MyUserDetailsService및 커스텀 패스워드 인코더(MyPasswordEncoder).

~하듯이WebSecurityConfigurerAdapter더 이상 사용되지 않습니다.이제 다음을 사용할 수 있습니다.

@Bean
public AuthenticationManager authenticationManager(HttpSecurity http) throws Exception {
    return http.getSharedObject(AuthenticationManagerBuilder.class)
            .build();
}

"mvn clean package" 명령을 실행하여 응용 프로그램을 재시작할 수 있습니다.

org.springframework 유형의 빈을 정의하는 것을 검토합니다.security.core.userdetails를 참조해 주세요.[ User Details ](사용자 상세)

언급URL : https://stackoverflow.com/questions/52243774/consider-defining-a-bean-of-type-org-springframework-security-authentication-au

반응형