# project-demo **Repository Path**: ftwv31/project-demo ## Basic Information - **Project Name**: project-demo - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-02-26 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 集成统一登录的方法: ## 包结构说明: **com.goldgov.authentication** :为扩展的登录方式 *DefaultAccountUserDetailsServiceImpl* 为默认登录的账号检查实现类 *MockLoginAuthenticationProvider* 为模拟登录的扩展实现类 **com.goldgov.oauth2filter** : 为过滤器相关类 *Oauth2Controller* 为oauth2认证通过后的回调类 *PreRequestFilter* 为登录前的过滤器,用于跳转统一认证服务器,对于特别的请求地址,通过配置文件方式处理,会走标准认证处理 **com.goldgov.config** : 为相关配置类 *FilterProperties* 为过滤器配置参数类 *SecurityExt* 为标准认证扩展接口实现类,用于增加filter和扩展登录方式 *WebMvcConfig* 为MVC配置类,用于定义界面跳转view关系 *FilterConfig* 为Oauth2Filter配置类,用于初始化并加载相关类文件 **resources下的界面文件** *static* 为静态文件,例如js *templates* 为自动跳转界面 ## 使用方式: POM.XML增加如下配置 ``` com.goldgov kduck-module 1.0.20030222-SNAPSHOT com.goldgov kduck-security 1.0.20030222-SNAPSHOT org.springframework.boot spring-boot-starter-thymeleaf 2.1.9.RELEASE ``` ## 配置参数说明: ``` kduck: oauth2-filter: ignored: - "/**/oauth/callback" - "/**/login" - "/**/**/static/**/**" oauth2-server: http://autherserver.sso.com:8080/login client-id: 7VQrEVZK139FYcSZuGOv9O2f client-secret: e10adc3949ba59abbe56e057f20f883e redirect-uri: http://localhost:8081/api-demo/oauth/callback ``` ## 前后端分享登录请求地址 使用Basic登录方式 ``` Get http://127.0.0.1:8081/api-demo/currentUser Authorization: Basic YWRtaW46ZTEwYWRjMzk0OWJhNTlhYmJlNTZlMDU3ZjIwZjg4M2U= ``` ### 登录失败/未登录 http status 为 401 ``` { "data":{ "timestamp":1583146212510, "status":401, "error":"Unauthorized", "message":"Unauthorized", "path":"/api-demo/currentUser" }, "code":-1, "message":"Unauthorized" } ``` ### 登录成功 http status 为 200 ``` { "data":{ "password":null, "username":"admin", "authorities":[ { "authority":"USER" } ], "accountNonExpired":true, "accountNonLocked":true, "credentialsNonExpired":true, "enabled":true }, "code":0, "message":"Authorized" } ``` ### 登录用户信息扩展 如果需要封装额外的登录信息到认证用户对象或session中,中可以扩展AuthUserInfoAdditional接口, 并声明为一个Spring的Bean即可。 ### 微信扫码登录 扫码登录流程如下: ``` +-----------+ 1 Process Login +---------------+ | +-------------------->+ | | Client | | Auth Server | 4 Callback | | 5 Success Callback | +<---------------+ | +<--------------------+ | | +----+------+ +---------------+ | ^ 2| Redirect Login Page | | v | | +-------+-------+ +---------+-----+ | | | 3 | | | Access Resources | Login Page +----->+ WeiXin | + | | | Scan Login | User | | | | +---------------+ +---------------+ ``` 当用户在未登录情况下访问受保护资源时,会被重定向到授权服务器的登录页面。 在登录页面中会(或链接)显示二维码,该登录二维码生成的URL地址,例如: ``` https://open.weixin.qq.com/connect/qrconnect?appid=wx806f25cd5430cc2a&redirect_uri=http%3a%2f%2fwww.xxxx.com&response_type=code&scope=snsapi_login&state=http%3a%2f%2fwww.xxxx.com#wechat_redirect ``` 链接参数说明: appid:需要填写真实的开发者账号的appid redirect_uri:微信扫码登陆成功后的回调地址,固定为授权服务器地址:http://主机:端口/wxlogin/callback(因为我没有具体测试账号,此步骤需要了解返回数据参数) state:页面从request的attribute中可以获取"original_uri"属性值来填充此参数 其他参数保持默认即可 开发者需要在授权服务器编写WxUserDetailsService接口实现类,并声明为Spring的Bean。 接口参数为微信返回的用户unionId,需要根据改ID查询并返回用户信息。 #### FIXME: 1 Oauth2Controller的callbace方法判断是否是微信登录的策略需要调整,因为目前没有真实测试账号,不知道参数名是什么。 2 微信二维码认证成功后的回调处理过滤器中获取unionId的参数名需要调整,因为目前没有真实测试账号,不知道参数名是什么。