IdentityServer4(二)

IdentityServer4(二)1 新建空的 Identity 项目 2 访问 localhost 5001 well known openid configuratio 访问 localhost 5001 connect token4 需要用 postman 访问 localhost 5001 connect token 要添加参数 不然会报错 错误信息 error invalid request 或者 error invalid scope 注意 要在 x www form urlenc

1、新建空的Identity项目

IdentityServer4(二)

2、访问localhost:5001/.well-known/openid-configuration

IdentityServer4(二)

 3、访问localhost:5001/connect/token

IdentityServer4(二)

 4、需要用postman 访问localhost:5001/connect/token,要添加参数,不然会报错,错误信息: “error”: “invalid_request”或者”error”: “invalid_scope”

注意:要在 x-www-form-urlencoded 中添加参数,在form-data 添加参数还是会报错

IdentityServer4(二)

5、需要修改config文件

public static class Config { public static IEnumerable 
  
    IdentityResources => new IdentityResource[] { new IdentityResources.OpenId() }; public static IEnumerable 
   
     ApiScopes => new ApiScope[] { new ApiScope("scope1"), new ApiScope("scope2"), }; public static IEnumerable 
    
      Clients => new Client[] { new Client { ClientId="myclient", ClientSecrets=new []{new Secret("secret".Sha256()) }, AllowedGrantTypes=GrantTypes.ResourceOwnerPasswordAndClientCredentials, AllowedScopes=new [] { "scope1" } } }; public static List 
     
       TestUsers => new List 
      
        { new TestUser { Username="pc", Password="123", SubjectId="1" } }; } 
       
      
     
    
  

6、需要修改Startup类的ConfigureServices方法

 public void ConfigureServices(IServiceCollection services) { // uncomment, if you want to add an MVC-based UI //services.AddControllersWithViews(); var builder = services.AddIdentityServer() .AddTestUsers(Config.TestUsers) .AddInMemoryApiScopes(Config.ApiScopes) .AddInMemoryClients(Config.Clients); // not recommended for production - you need to store your key material somewhere secure builder.AddDeveloperSigningCredential(); services.AddAuthentication(); }

7、另一种方式,不添加scopes

Config类修改

public static class Config { public static IEnumerable 
  
    IdentityResources => new IdentityResource[] { new IdentityResources.OpenId() }; public static IEnumerable 
   
     Clients => new Client[] { new Client { ClientId="myclient", ClientSecrets=new []{new Secret("secret".Sha256()) }, AllowedGrantTypes=GrantTypes.ResourceOwnerPasswordAndClientCredentials, AllowedScopes=new [] { "openid" } } }; public static List 
    
      TestUsers => new List 
     
       { new TestUser { Username="pc", Password="123", SubjectId="1" } }; } 
      
     
    
  

Startup类修改

 public void ConfigureServices(IServiceCollection services) { // uncomment, if you want to add an MVC-based UI //services.AddControllersWithViews(); var builder = services.AddIdentityServer() .AddInMemoryIdentityResources(Config.IdentityResources) .AddInMemoryClients(Config.Clients) .AddTestUsers(Config.TestUsers); // not recommended for production - you need to store your key material somewhere secure builder.AddDeveloperSigningCredential(); services.AddAuthentication(); }

备注:不添加ApiScopes  时,使用 IdentityResource 的openid

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/218510.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 上午7:04
下一篇 2026年3月18日 上午7:05


相关推荐

  • 初级算法学习步骤

    初级算法学习步骤前言零散整理一个多月终于整理完了。。。。这是一篇初级算法学习的步骤总结,或许可以帮助你怎么去接触算法阅读本文需要一定java语法基础和api文档查看基础,但算法其实看重的是思想而不是语言,所以都可以借鉴。本人大二,参加过蓝桥杯。一直没时间吧之前的总结整理出来,现在准备整理一下用java做算法的一些东西……学习了两个月左右算法,从啥都不会到小白再到算是初级……做一个总结,请高手多多指…

    2022年6月19日
    35
  • pycharm导入模块问题

    pycharm导入模块问题最近再次遇到这个问题 便想起来写个记录 以免再次遗忘 问题描述 自己手写代码时 可能遇到 在 Python 编写时 需要用到导入已创建好的模块 但一般会出现下图红方块问题 并没有要导入的模块的问题 即模块无法被识别 正在上传 重新上传取消 在使用别人写好的一组代码文件时 运行也会报错显示找不到对应模块 正在上传 重新上传取消转存失败

    2026年3月17日
    2
  • OpenClaw部署全解析:2026年自动化工具一键部署指南

    OpenClaw部署全解析:2026年自动化工具一键部署指南

    2026年3月13日
    1
  • jdbc是什么(jdbc五个步骤)

    测试工具:Pact、Janus、Pacto等。packagecom.example.demo;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublicclassDemoApplication{publicstaticvoidmain(Strin

    2022年4月14日
    55
  • centos开启关闭防火墙(电脑防火墙关闭有什么影响)

    CentOS6:1)永久性生效,重启后不会复原开启:chkconfigiptableson关闭:chkconfigiptablesoff 2)即时生效,重启后复原service iptables status    查看防火墙状态service iptables start     开启防火墙service iptab…

    2022年4月13日
    57
  • 关于kindeditor中如何设置富文本编辑器中的内容

    关于kindeditor中如何设置富文本编辑器中的内容关于kindeditor中如何设置富文本编辑器中的内容KindEditor.html(id,value);我们对Markdown编辑器进行了一些功能拓展与语法支持,除了标准的Markdown编辑器功能,我们增加了如下几点新功能,帮助你用它写博客:全新的界面设计,将会带来全新的写作体验;在创作中心设置你喜爱的代码高亮样式,Markdown将代码片显示选择的高亮样式进行展示;增加了…

    2022年10月9日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号