jojozhai / security

586 stars 488 forks source link

AuthenticationManager无法注入,是要自己实现么 #10

Open pengliangs opened 6 years ago

pengliangs commented 6 years ago

@Autowired private AuthenticationManager authenticationManager;

pengyand commented 5 years ago

Spring 2.x 好像不能注入了, 解决方案:

在WebSecurityConfigurerAdapter的实现类当中,重写authenticationManagerBean方法:

@Bean(name = BeanIds.AUTHENTICATION_MANAGER) @Override public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } 但是会导致安全配置都失效~~~ 提示:{"timestamp":1546938283602,"status":401,"error":"Unauthorized","message":"Unauthorized","path":"/oauth/token"}%

pengyand commented 5 years ago

问题解决了: @Configuration public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

/**
 * Spring 2.0 不会自动注入这个实例了
 * @return
 * @throws Exception
 */
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
}

}

momokanni commented 5 years ago

也可以构造器注入

public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter{
       /**
     * 构造函数注入
     */
    private AuthenticationManager authenticationManager;
       /**
     * 注入AuthenticationManager
     * @param authenticationConfiguration
     * @throws Exception
     */
    public AuthorizationServerConfig(AuthenticationConfiguration authenticationConfiguration) throws Exception {

        this.authenticationManager = authenticationConfiguration.getAuthenticationManager();
    }