oauth2 token为空拦截_配合OAuth2进行单设备登录拦截

本文介绍了如何通过OAuth2实现单设备登录,当用户在其他地方登录时,本地操作将被拦截并重定向到登录界面。关键在于利用Redis存储Session,并在登录时获取access_token。在Controller中,通过FeignClient调用OAuth2登录接口,成功后将Session信息存入Redis。同时,配置拦截器检查每次请求的Session,与Redis中存储的Session进行对比,确保一致性以实现登录拦截。
摘要由CSDN通过智能技术生成

要进行单设备登录,在其他地点登录后,本地的其他操作会被拦截返回登录界面。

原理就在于要在登录时在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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值