31.密码模式演示
1.修改授权服务器配置
AuthorizationServerConfig.java
/**
* @author etern
* @title: AuthorizationServerConfig
* @projectName springsecurityoauth2-demo
* @description: TODO 授权服务器配置
* @date 2022/2/17 15:57
*/
//配置类注解
@Configuration
//启用授权服务器注解
@EnableAuthorizationServer
//继承AuthorizationServerConfigurerAdapter类
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
//注入自定义的密码配置
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private UserService userService;
/**
* @return
* @throws
* @Author etern
* @Description //TODO 使用密码模式所需配置
* @Date 14:29 2022/2/19
* @Param * @param endpoints
**/
//重写configure中参数是endpoints
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
//密码授权模式是把密码直接传给授权服务器的
endpoints.authenticationManager(authenticationManager)
.userDetailsService(userService);
}
//重写configure中参数是clients
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
//配置client-id
.withClient("admin")
//配置client-secret
.secret(passwordEncoder.encode("112233"))
//配置访问token的有效时间(秒)
//.accessTokenValiditySeconds(3600)
//配置刷新token的有效时间(秒)
//.refreshTokenValiditySeconds(864000)
//配置redirect-uri,用于授权成功后跳转
.redirectUris("http://www.baidu.com")
//配置申请的权限范围
.scopes("all")
//配置grant-type,表示授权类型(授权码模式)
//.authorizedGrantTypes("authorization_code");
//授权模式:密码模式
.authorizedGrantTypes("password");
}
}
2.SecurityConfig.java中添加一个注入授权的bean
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 Eternal Night
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果