Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client-credentials 项目中,如何获取到redis 某个值的user 信息尼,或者如何判定user与token 是对应的尼 #18

Open
lsl80 opened this issue Jul 10, 2018 · 12 comments

Comments

@lsl80
Copy link

lsl80 commented Jul 10, 2018

client-credentials 项目中,如何获取到redis 某个值的user 信息尼,或者如何判定user与token 是对应的尼

@lexburner
Copy link
Owner

spring-security-oauth2 没有提供这样的接口。

@lsl80
Copy link
Author

lsl80 commented Jul 11, 2018

可否 给个思路尼,怎么判定user与token 是对应的,

@ufosaga
Copy link

ufosaga commented Jul 19, 2018

@lisl2017 可以这么搞:

    @RequestMapping(value = "/user/queryByToken", produces = "application/json;charset=UTF-8")
    public String queryByToken() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null || (authentication instanceof AnonymousAuthenticationToken)) {
            break;
        }

        String userEmail = authentication.getName();
        System.out.println(userEmail);

        ...
    }

@ufosaga
Copy link

ufosaga commented Jul 19, 2018

这个接口在oauth2授权成功后调用, authentication.getName() 返回的就是你的 user 的用户名, 或者邮箱

@lexburner
Copy link
Owner

authentication.getName() 这是根据请求过来的 token 找到对应的用户,不是根据 username 找到对应的 token,方向相反。

@lsl80
Copy link
Author

lsl80 commented Jul 23, 2018

非常感谢,再请教个问题:密码模式下oauth2 接口调用如何保证安全性尼,我看都是直接传值,可否根据时间戳加签名尼,或者md5,或者对称不对称等等加密方式

@lexburner
Copy link
Owner

@lisl2017 oauth2 本身就是用来保证接口安全性的,访问受限接口时需要携带 oauth2 产生的 token,否则认为是无效请求。你是否是担心申请 oauth2 的 token 时密码泄露?

@lsl80
Copy link
Author

lsl80 commented Jul 24, 2018

是的,比如在前后端 分离商城场景中,如何保证申请token时的安全

@lexburner
Copy link
Owner

你这个场景不是一般意义的服务间接口调用,我不推荐使用 OAuth2。

@lexburner
Copy link
Owner

@lisl2017 当前页面是否有权利调用对应的接口,应当是由当前登录的用户的 sessionId,取到对应的用户信息+权限,来做判断。浏览器端我一向推崇使用 session + cookie 的机制。如果你一定要使用 OAuth2,可以尝试将 token 理解为 sessionId,保存在 cookie 或者 localStorge 中,不需要考虑 token 泄露的问题。

@lsl80
Copy link
Author

lsl80 commented Jul 24, 2018

哦,估计放在localstorge 可行 ,token 生成原理是啥,可以保证永不重复么

@lexburner
Copy link
Owner

@lisl2017 token 就是 uuid,建议去看看 spring-security-oauth2 的基础 :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants