IdentityServer4实现Oauth2.0四种模式之隐藏模式

 

接上一篇:IdentityServer4实现OAuth2.0四种模式之密码模式,密码模式将用户的密码暴露给了客户端,这无疑是不安全的,隐藏模式能够解决这个问题,由用户本身在IdentityServer服务器进行登陆验证,客户端不须要知道用户的密码。javascript

一,服务端配置

1,添加客户端:在IentityServer项目添加一个客户端用于支持隐藏模式访问。隐藏模式只须要用户登陆验证,不须要客户端密码。

IdentityServer.Config.Getlientshtml

public static IEnumerable<Client> GetClients()
        {
            return new Client[] {
              
                new Client()
                {
                    //客户端Id
                     ClientId="apiClientCd",
                     //客户端密码
                     ClientSecrets={new Secret("apiSecret".Sha256()) },
                     //客户端受权类型,ClientCredentials:客户端凭证方式
                     AllowedGrantTypes=GrantTypes.ClientCredentials,
                     //容许访问的资源
                     AllowedScopes={
                        "secretapi"
                    }
                },
                new Client()
                {
                    //客户端Id
                     ClientId="apiClientPassword",
                     //客户端密码
                     ClientSecrets={new Secret("apiSecret".Sha256()) },
                     //客户端受权类型,ResourceOwnerPassword:用户名密码模式
                     AllowedGrantTypes=GrantTypes.ResourceOwnerPassword,
                     //容许访问的资源
                     AllowedScopes={
                        "secretapi"
                    }
                }
                ,
                new Client()
                {
                    //客户端Id
                     ClientId="apiClientImpl",
                     ClientName="ApiClient for Implicit",
                     //客户端受权类型,Implicit:隐藏模式
                     AllowedGrantTypes=GrantTypes.Implicit,
                     //容许登陆后重定向的地址列表,能够有多个
                    RedirectUris = {"https://localhost:5002/auth.html" },
                     //容许访问的资源
                     AllowedScopes={
                        "secretapi"
                    },
                     //容许将token经过浏览器传递
                     AllowAccessTokensViaBrowser=true
                }
            };
        }

2,添加IdentityServer的ui模板。

微软为IdentityServer4建立了一系列的模板,能够在命令行中使用dotnet new -IdentityServer4.Templates安装。而后在IdentityServer项目根据目录下打开命令行,运行dotnet new is4ui 安装IdentityServer的ui模板。会自动添加Quickstart、wwwroot、Views三个文件夹到此目录前端

 

  

添加好ui模板后,还须要启用IdentityServer项目的mvc功能。修改IdentityServer.Startup.ConfigureServices,添加一行代码java

  services.AddMvc();

修改IdentityServer.Startup.Configure,添加二行代码后端

          //访问wwwroot目录静态文件
            app.UseStaticFiles();
            //使用Mvc中间件
            app.UseMvcWithDefaultRoute();

二,Mvc客户端配置

添加跳传页面api

在第一步的客户端实例化中配置了RedirectUris = {"https://localhost:5002/auth.html" },这是一个跳转页面,用户在IdentityServer上登陆成功后将会带着access_token自动跳转到这个页面。如今这个页面尚未建立。浏览器

在IdentityMvc项目的wwwroot目录下建立一个名为auth的html页面。用于redirect_uri。若是uri瞄点中带有token,把token显示在页面上。安全

IdentityMvc/wwwroot/auth.html服务器

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script type="text/javascript">
        var token = null;
        window.onload = function () {
            var url = window.location.href;

            var array = url.split("#");
            if (array.length > 1) {
                token = array[1];
             document.getElementById("content").innerHTML = token;
            }
        }
    </script>
</head>
<body>
    <div id="content"></div>
</body>
</html>

三,获取access_token

根据OAuth2.0协议,隐藏模式须要传的参数以下所示。mvc

client_id:客户端Id
redirect_uri=重定向Url,用户登陆成功后跳回此地址
response_type=token,固定值,表示获取token
scope=secretapi,此token须要访问的api

接受参数的地址则是IdentityServer的Discover文档中的authorization_endpoint节点。把参数和地址拼接成如下地址:http://localhost:5000/connect/authorize?client_id=apiClientImpl&redirect_uri=https://localhost:5002/auth.html&response_type=token&scope=secretapi,直接访问,会跳转到用户登陆页面

 

 

 

 

 使用以前添加的用户:apiUser登陆,确认受权访问secretapi这个api资源

 

 确认后,浏览器将会自动跳转到redirect_url,也就是咱们第一步创建的html页面。

发现token已经被打印出来了。使用这个token用于Bearer受权验证就能够访问标识为secretapi的api资源。访问一下以前的测试接口。

 

 

 隐藏模式解决了客户端模式用户身份验证和受权的问题,也解决了密码模式面临的用户密码暴露的问题,适应于全前端没有后端的第三方应用。但因为token携带在url中,安全性方面不能保证。下一篇讲的受权码模式在安全性方面作得更好。

相关文章
相关标签/搜索