在OAuthServerConfig.java里下面的配置,第一个是把token相关信息保存在redis里,下面一个是把相关token保存在mysql里

 @Bean

    public TokenStore tokenStore() {

        return new RedisTokenStore(redisConnectionFactory);

    }

   /*

    @Bean

    public TokenStore tokenStore() {

        return new JdbcTokenStore(dataSource);

    }*/

es_member  表里的member_name对应用户,通过这个不同的用户与密码来获得token

oauth_client_details  这个是认证参数 ,以后可以不同的企业有不同的client_id与相关参数

 

相应的token是保存在redis

对于密码password模式的话,postman只要下面就可以了,这里用户或密码包括client都可以不同

authorization要No Auth

postman里用post提交这个也一样

在通过上面的token获得相关api操作

命令方式如下

curl -i -X POST -d "username=nbacheng&password=111111&grant_type=password&client_id=app&client_secret=1"

curl -i http://localhost:8080/api/protect/getUserList -H "Authorization: Bearer 729b1d11-d537-4c49-a445-193c2c3205d6"

上面的根据client不同的参数,会返回不同的参数

但开始swagger-UI一直调试有问题,后来在Swagger2Config里 ,特别是下面 ticketPar.name("Authorization")  这个必须是Authorization

public Docket createRestApi() {

ParameterBuilder ticketPar = new ParameterBuilder();

List<Parameter> pars = new ArrayList<Parameter>();

ticketPar.name("Authorization").description("认证token")

.modelRef(new ModelRef("string")).parameterType("header")

.required(false).build(); //header中的ticket参数非必填,传空也可以

pars.add(ticketPar.build());    //根据每个方法名也知道当前方法在设置什么参数

return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()

.apis(RequestHandlerSelectors.basePackage("com.birdsh.lqy")).paths(PathSelectors.any())

.build()

.globalOperationParameters(pars);

    }

上面同时在所有api里都加上token参数了