Spring boot OAuth2 密码授权模式问题

sinat_34144664 2017-04-11 03:50:45
这是OAuth2 服务端
如果要使用密码授权模式 就要用到这个 AuthenticationManager
可是一添加 Qualifier标签就报错, 我看网上能成功运行的 代码 都是要加这个Qualifier标签的.


@Configuration
@EnableAuthorizationServer //配置 授权服务

public class OAuth2Config extends AuthorizationServerConfigurerAdapter {

/**
* test
* 为了使用“password”授权方式,我们需要通过spring的@Autowired注解来注入和使用AuthenticationManager bean
* @return
*/


@Autowired //去掉 Autowired 不支持 grant_type =passwpd (添加后: invalid_grant Bad credentials)
@Qualifier("authenticationManagerBean") //添加后 启动报错,注入不了
private AuthenticationManager authenticationManager;



/**/
@Bean
public TokenStore tokenStore(){
return new InMemoryTokenStore();
}



@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory().withClient("tonr").secret("secret")
.autoApprove(true) //test
.authorizedGrantTypes("password", "authorization_code", "client_credentials")
.scopes("read");
// .accessTokenValiditySeconds(120).//Access token is only valid for 2 minutes.
// refreshTokenValiditySeconds(600);//Refresh token is only valid for 10 minutes.
// .authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit" client_credentials)
}

/**
* test
* @param security
* @throws Exception

@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security.tokenKeyAccess("permitAll()")
.checkTokenAccess("isAuthenticated()");
}
*/

/**
* test
* @param endpoints
* @throws Exception
*/
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.tokenStore(tokenStore()).authenticationManager(authenticationManager);

}


}


这是 授权资源端:
@Configuration
@EnableResourceServer // 配置授权资源路径
@Order(6)
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

/**
* test

@Autowired
TokenStore tokenStore;
*/
@Override
public void configure(ResourceServerSecurityConfigurer resources) {
resources.resourceId("app").stateless(false);//.tokenStore(tokenStore);
}

@Override
public void configure(HttpSecurity http) throws Exception {
http
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
.and()
.requestMatchers().antMatchers("/se")
.and()
.authorizeRequests()
.antMatchers("/se").access("#oauth2.hasScope('read')");
}



}


这是

@EnableAuthorizationServer
@Order(2)
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {


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

@Autowired
public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user").password("user").roles("USER");
}

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

http.authorizeRequests()
.antMatchers("/").permitAll()
.anyRequest().authenticated()
//.and()
// .formLogin()
.and()
.httpBasic()

;
}


}
...全文
8959 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
懂一点点 2017-07-07
  • 打赏
  • 举报
回复
在继承WebSecurityConfigurerAdapter这个类的那个类里增加 @Override @Bean // share AuthenticationManager for web and oauth public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); }
sinat_34144664 2017-04-11
  • 打赏
  • 举报
回复
如果去掉
@Qualifier标签,
能启动运行,但是 进行 密码授权模式就会 弹出



然后控制台 空指针异常.不知道哪里空指针




我配置的 客户端授权模式 和授权码授权模式 都可以用, 就是这个 密码授权模式出问题了
sinat_34144664 2017-04-11
  • 打赏
  • 举报
回复
以上代码 添加了 Qualifier标签就报错误



81,095

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧