对外接口
项目中对外的接口一般来说只有两个,一个是用户认证获取用户信息,另一个就是登出时删除token。代码如下:
package com.shareprog.auth.controller;
import java.security.Principal;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import lombok.AllArgsConstructor;
/**
* @ClassName: AuthController
* @Description: 权限控制层
* @author cl
* @date 2021年1月19日
*/
@RestController
@AllArgsConstructor
public class AuthController {
private final ConsumerTokenServices consumerTokenServices;
/**
*
* @Title: getUser
* @Description: 获取用户信息
* @param principal 用户信息
* @return
*/
@RequestMapping("/oauth/user")
@PreAuthorize("hasAuthority('role')")
public Principal getUser(Principal principal) {
return principal;
}
/**
* @Title: logout
* @Description: 登出操作,删除token
* @param token 令牌
* @return
*/
@SuppressWarnings("rawtypes")
@GetMapping("/oauth/logout")
public ResponseEntity logout(String token) {
if (consumerTokenServices.revokeToken(token)) {
return ResponseEntity.ok().build();
}
return ResponseEntity.badRequest().build();
}
}