密码模式
在配置类
认证服务器配置中配置
重写参数为AuthorizationServerEndpointsConfigurer
的方法
@Override
//授权服务器端点配置
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
}
在其中注入用户详情类(继承UserDetailService
的类);
再注入授权管理器->这个需要我们在配置权限管理的类(继承WebSecurityConfigurerAdpter
的类)那里进行重写authenticationManagerBean
方法
将这个bean返回给spring就可以了;
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
完成后继续回到刚才的位置将这个bean注入进去交给认证管理器;
@Autowired
private UserDetailsService userDetailsService;
@Autowired
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints
//自定义登录逻辑,我们把之前自定义的登录逻辑类注进来填进去(实现UserDetailService的类)
.userDetailsService(userDetailsService)
//配置授权管理器
.authenticationManager(authenticationManager);
}
然后我们回到配置每个商家账号信息配置那里给他加上一个密码模式,【可以直接存在数据库里】到时候直接加载进来了
icdsb.withClient(partner.getAppId())
.secret(partner.getAppSecret())
.authorizedGrantTypes(partner.getAuthorizedGrantTypes())
//.authorizedGrantTypes("authorization_code","password") <=x-ray
然后请求方式也要变更
改为直接使用用户的账户密码进行获取token,
省去了用授权码部分;原来的请求授权码部分取消,直接用用户名密码取得他的token;
替代了原先的appid
以及secret
,模式为密码模式
总结:
总之就是原来要先获取授权码,然后携带商家登录参数和用户授权码获取token;
密码模式改成了直接携带商家登录参数,然后在表单里加上用户的账户密码,进行获取token;取消了原来使用授权码时appid的填写(其实我本来没填写的,我寻思商户登录不就有了吗,而且也成功获取到了,真是奇怪);