GithubHelp home page GithubHelp logo

lichuangbo / forum Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 2.96 MB

星月技术论坛(前后端)

Java 56.46% HTML 11.68% JavaScript 13.74% TSQL 0.76% CSS 17.36%
bootstrap oauth2 redis spring-boot mybatis mysql

forum's Introduction

社区论坛

  1. 创建Spring Boot项目,导入坐标
  2. 初始化仓库,并托管到GitHub上,使用git提交

GitHub登记入口

BootStrap组件

论坛参考

GitHub登记

OKhttp 发送post请求

GitHub API文档

Spring Boot + MyBatis

flyway

lombok坐标

IDEA安装lombok插件

GitHub登录流程

> 第三方登录,要OAuth授权。如用户想登录论坛,但是论坛让用户提供第三方网站的数据,这样用户可以不用注册新账号,就可以证明自己的身份
  1. 要获取OAuth授权,必须去GitHub上登记,填写登记表
  2. 用户访问论坛,点击登录,调用authorize接口去访问github(此时要在url中携带client_id和redirect_uri)
  3. 用户进入github会要求用户登录,此时用户可以选择登录;登录成功后,github会询问用户,该应用在请求用户数据,询问是否授权
  4. github拿到用户权限回调,跳转到指定的redirect-uri地址,同时携带授权码code
  5. 论坛拿到授权码,调用access_token接口,并且携带code去访问github,此次访问是想获取请求令牌
  6. github响应,并返回一段JSON数据,里面包含了令牌access_token
  7. 拿到令牌,论坛携带access_token,调用GitHub api请求用户的真实数据
  8. 论坛将用户信息存入数据库,更新登录状态

携带client_id和redirect_uri去请求用户的github身份 GET https://github.com/login/oauth/authorize?client_id=xx&redirect_uri=xx&scope=user&state=yy

GitHub根据redirect_uri重定向回站点,同时携带授权码code

携带授权码访问access_token接口,请求令牌 POST https://github.com/login/oauth/access_token 请求体:client_id=xx&client_secret=xx&code=xx 获取到的格式如access_token=e72e16c7e42f292c6912e7710c838347ae178b4a&token_type=bearer,要截取出有用的

携带令牌访问user接口,获取用户真实数据 GET https://api.github.com/user?access_token=xxx 获取到用户信息,是JSON格式的,很多只拿想要的信息

持久化登录状态

  1. 分析:先前生产环境下,每一次服务器重新启动,用户都需要去点击登录。这主要是因为在后台session中存储的是gitHubUser 这个对象,这个是根据令牌获取到的。只要服务器一重新启动,这个对象就为空了,在前台页面中他就是根据session域中的user是否 为空来做逻辑判断的。 因此必须要将登录状态持久化到数据库中!!! 前台逻辑不去碰,还让他去判断session域; 从后台入手,铁定不能存gitHubUser了,要存自己的持久化的user对象,借助cookie去实现
  2. 实现步骤
    1. 用户登录成功,即gitHubUser不为空,就将它存储到数据库中;存完后,添加一个cookie信息,里边是自己生成的token
    2. (此时用户登录成功,我们是让他重定向到根路径/下)来到indexController,之前没有任何逻辑只是简单跳转,现在在 方法中遍历cookie信息,查找名为token的cookie
    3. 根据value去数据库中查找该用户,如果此时查出来的user不为空,再将session中存储user(我们将第一步的存session 延迟到了第三步,中间夹杂了持久化操作)

异常处理

  1. Spring Boot自动提供了一种/error的错误信息映射,出现错误会自动去找error.html,但是他不能处理404/500这样的异常 如果我们想要利用好他的映射机制,需要自己去实现,在类上配上@ControllerAdvice注解和处理的异常类型@ExceptionHandler(xxx), 想要拿到信息并在客户端展示,也可以添加Model域。这样Spring Boot会自动为进行映射
  2. 拦截像404/500这样的异常 必须自己写Controller,路径也必须是/error,在指定拦截的方法上getMapping注解要加上produces明确指出是html的 还是JSON格式的。在方法的内部通过提供的方法提取status,根据status(如is4xxClientError)在Model域中添加错误信息

forum's People

Contributors

lichuangbo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.