要进行单设备登录,在其他地点登录后,本地的其他操作会被拦截返回登录界面。
原理就在于要在登录时在redis中存储Session,进行操作时要进行Session的比对。
具体实现,假设我们的OAuth 2的登录调用接口如下:
共享Session,User模块跟OAuth模块都要设置
@Configuration
@EnableRedisHttpSession
public class SessionConfig {
}
Feign
@Component
@FeignClient("oauth-center")
public interface Oauth2Client {
/**
* 获取access_token
* 这是spring-security-oauth2底层的接口,类TokenEndpoint
*
* @param parameters
* @return
* @see org.springframework.security.oauth2.provider.endpoint.TokenEndpoint
*/
@PostMapping(path = "/api-o/oauth/token")
Map postAccessToken(@RequestParam Map parameters);
/**
* 删除access_token和refresh_token
* 认证中心的OAuth2Controller方法removeToken
*
* @param access_token
*/
@DeleteMapping(path = "/api-o/remove_token")
void removeToken(@RequestParam("access_token") String access_token);
}
Controller
/**
* Created by Administrator on 2018/10/19.
*/
@Slf4j
@RestController
public class UserTokenController {
@Autowired
private Oauth2Client oauth2Client;
@Resource
private RedisService redisServiceImpl;
/**
* 系统登陆
* 根据用户名登录
* 采用oauth2密码模式获取access_token和refresh_token
*
* @param loginParam
* @return
*/
@Po