当前位置: 首页 > news >正文

长沙网站优化外包公司住房和城乡规划建设局官方网站

长沙网站优化外包公司,住房和城乡规划建设局官方网站,lnmp wordpress,边坝网站制作Spring Boot整合OAuth2#xff0c;实现GitHub第三方登录 1、第三方登录原理 第三方登录的原理是借助OAuth授权来实现#xff0c;首先用户先向客户端提供第三方网站的数据证明自己的身份获取授权码#xff0c;然后客户端拿着授权码与授权服务器建立连接获得一个Access Token…Spring Boot整合OAuth2实现GitHub第三方登录 1、第三方登录原理 第三方登录的原理是借助OAuth授权来实现首先用户先向客户端提供第三方网站的数据证明自己的身份获取授权码然后客户端拿着授权码与授权服务器建立连接获得一个Access Token之后客户端就可以通过Access Token来与资源服务器进行交互。 使用OAuth的好处是提供给用户一个特定的密钥用户持有这个密钥可以访问应用中的任何信息而不需要向网站提供用户名密码可以实现跨系统共享用户授权协议。 通过控制用户持有的密钥可以很方便的控制用户可以访问的资源以及控制密钥的过期时间。 以下是来自维基百科对于OAuth的介绍 开放授权OAuth是一个开放标准允许用户让第三方应用访问该用户在某一网站上存储的私密的资源如照片视频联系人列表而无需将用户名和密码提供给第三方应用。 OAuth允许用户提供一个令牌而不是用户名和密码来访问他们存放在特定服务提供者的数据。每一个令牌授权一个特定的网站例如视频编辑网站)在特定的时段例如接下来的2小时内内访问特定的资源例如仅仅是某一相册中的视频。这样OAuth让用户可以授权第三方网站访问他们存储在另外服务提供者的某些特定信息而非所有内容。 OAuth是OpenID的一个补充但是完全不同的服务。 交互流程如下 2、GitHub实现第三方登录 首先需要在github中对应用进行登记让Github知道谁在发送请求。 访问这个网址填写登记表 提交成功之后GitHub会返回Client ID Client Secrets 这是应用的身份识别码 创建一个SpringBoot工程pom.xml文件内容如下 ?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.7.17/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdorg.pp/groupIdartifactIdspringboot-oauth2-api/artifactIdversion0.0.1-SNAPSHOT/versionnamespringboot-oauth2-api/namedescriptionspringboot整合oauth2实现GitHub第三方登录/descriptionpropertiesjava.version1.8/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-thymeleaf/artifactId/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdconfigurationexcludesexcludegroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/exclude/excludes/configuration/plugin/plugins/build/project将ID和密钥添加到配置文件application.yml中 # 项目端口号 server:port: 8080 # GitHub认证相关参数 github:client:id: xxxsecret: xxx创建一个实体类用于映射授权成功产生的Token令牌 import com.fasterxml.jackson.annotation.JsonProperty; /**** Token令牌 - 响应参数** author supanpan* date 2023/10/25*/ public class AccessTokenResponse {JsonProperty(access_token)private String accessToken;public String getAccessToken() {return accessToken;}public void setAccessToken(String accessToken) {this.accessToken accessToken;} }OAuthController如下 *** author supanpan* date 2023/10/25*/ Controller public class OAuthController {Value(${github.client.id})private String clientId;Value(${github.client.secret})private String clientSecret;GetMapping(/oauth/redirect)public String handleRedirect(RequestParam(code) String requestToken, Model model) {// 使用RestTemplate来发送HTTP请求RestTemplate restTemplate new RestTemplate();// 获取Token的UrlString tokenUrl https://github.com/login/oauth/access_token ?client_id clientId client_secret clientSecret code requestToken;// 使用restTemplate向GitHub发送请求获取TokenAccessTokenResponse tokenResponse restTemplate.postForObject(tokenUrl, null, AccessTokenResponse.class);// 从响应体中获取Token数据String accessToken tokenResponse.getAccessToken();// 携带Token向GitHub发送请求String apiUrl https://api.github.com/user;HttpHeaders headers new HttpHeaders();headers.set(Authorization, token accessToken);HttpEntityString entity new HttpEntity(parameters, headers);ResponseEntityString response restTemplate.exchange(apiUrl, HttpMethod.GET, entity, String.class);model.addAttribute(userData, response.getBody());return welcome;} }SpringBoot启动器 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;SpringBootApplication public class SpringbootOauth2ApiApplication {public static void main(String[] args) {SpringApplication.run(SpringbootOauth2ApiApplication.class, args);}}还需要编写两个html页面index.html和welcome.html index.html !DOCTYPE html htmlheadmeta charsetutf-8 /meta http-equivX-UA-Compatible contentIEedgetitleOAuth2 Demo/titlemeta nameviewport contentwidthdevice-width, initial-scale1 /headbody a idloginLogin with GitHub/ascriptconst client_id xxxx;const authorize_uri https://github.com/login/oauth/authorize;const redirect_uri http://localhost:8080/oauth/redirect;const link document.getElementById(login);link.href ${authorize_uri}?client_id${client_id}redirect_uri${redirect_uri}; /script/body/htmlwelcome.html !DOCTYPE html html langen xmlns:thhttp://www.thymeleaf.orgheadmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0meta http-equivX-UA-Compatible contentieedgetitleHello/title /headbodyh1Welcome/h1div th:text${userData}/div /body/html启动项目浏览器访问localhost:8080会跳转到index页面点击链接会跳转到GitHub应用授权页面 点击跳转到GitHub授权之后GitHub会询问示例代码正在请求数据您是否同意授权。 用户同意授权 GitHub 就会跳转到redirect_uri指定的跳转网址并且带上授权码跳转回来的 URL 就是下面的样子 // code参数就是授权码 http://localhost:8080/oauth/redirect?code:4ea423f2ec1e04c6376a如下是服务的响应数据 access token: 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 {login: AtwoodPa,id: 110728122,node_id: U_kgDOBpmTug,avatar_url: https://avatars.githubusercontent.com/u/110728122?v4,gravatar_id: ,url: https://api.github.com/users/AtwoodPa,html_url: https://github.com/AtwoodPa,followers_url: https://api.github.com/users/AtwoodPa/followers,following_url: https://api.github.com/users/AtwoodPa/following{/other_user},gists_url: https://api.github.com/users/AtwoodPa/gists{/gist_id},starred_url: https://api.github.com/users/AtwoodPa/starred{/owner}{/repo},subscriptions_url: https://api.github.com/users/AtwoodPa/subscriptions,organizations_url: https://api.github.com/users/AtwoodPa/orgs,repos_url: https://api.github.com/users/AtwoodPa/repos,events_url: https://api.github.com/users/AtwoodPa/events{/privacy},received_events_url: https://api.github.com/users/AtwoodPa/received_events,type: User,site_admin: false,name: null,company: null,blog: ,location: null,email: null,hireable: null,bio: null,twitter_username: null,public_repos: 6,public_gists: 0,followers: 0,following: 3,created_at: 2022-08-06T13:02:16Z,updated_at: 2023-09-03T00:15:55Z } authorization code: 4ea423f2ec1e04c6376a成功执行上述流程最终展示示例的welcome页面 到这里Spring Boot整合GitHub实现第三方登录的实现就结束了以此类推其他厂商的第三方登录实现流程也大概是这样。
http://www.dnsts.com.cn/news/83064.html

相关文章:

  • 网站搭建需要多少钱最近国际新闻大事
  • 网站美工主要工作是什么温岭建设规划局网站
  • 网站 设计 深圳怀柔青岛网站建设
  • 免费永久网站制作襄阳万家灯火网站建设
  • 潍坊程序设计网站建设公司李佳琦网络营销方式
  • 中国网站建设新闻泉州微信网站建设公司
  • 怎么做卖花的网站商品网站策划书
  • 学做软件的网站上海哪里做网站
  • wordpress 关联表杭州seo顾问
  • 花店网站建设论文企业网站建设 新闻宣传
  • 做网站用百度地图和天地图三屏合一网站建设
  • 中山网站建设中山花都网站推广
  • 怎样设置网站主域名ssh做电商 网站
  • 有没有专门帮人做图的网站人际网络营销能做吗
  • 画廊网站模板 frontpage网页ui设计网站
  • 模仿网站侵权吗女生做a视频的网站是什多少
  • 手机网站制作解决方案珠海市住房城乡建设局网站
  • 东阳市网站建设做调查问卷的网站
  • 没有网站怎么做百度竞价wordpress 首页判断
  • 什么是营销型的网站推广柳州网站建设优化推广
  • 广安 网站建设物流网站模板
  • 有网站源码怎么搭建网站营销型企业、公司网站案例
  • 购书网站开发的意义内容网站设计范例
  • 昆明参差网站支持asp的免费空间 适合钓鱼网站
  • app网站建设 - 百度网站搭建怎么收费呀
  • 网站建设单元格边距荆州市做网站的
  • 网站建设与管理自考本专业搜索服务网络公司
  • 网站设计是用ps做图吗为什么建设网站很多公司没有
  • 做零售去哪个外贸网站wordpress产品筛选
  • python做网站 要学多久网站管理模式