QQ、微信登录扫码接入

本文详细介绍了如何通过QQ互联和微信公众号实现网页端的扫码登录,包括PC端和手机端授权过程,涉及开发者注册、应用配置、OAuth授权码和access_token获取等关键步骤。
摘要由CSDN通过智能技术生成

一、QQ(网页pc端扫码登录):

1.打开QQ互联:QQ互联管理中心

2.申请公司开发者

3.创建网站应用,前端页面QQ登录样式参考:Client Flow Example - JSSDK

4.查看QQapi文档:使用Authorization_Code获取Access_Token — QQ互联WIKI

二、微信公众号(网页pc端扫码登录):

1.需要企业的公众号号(服务号)

2.登录公众号以后需要配置token:

https://blog.csdn.net/qq_20382175/article/details/136808195?spm=1001.2014.3001.5502

3.调取微信生成二维码接口:

4.获取到微信回传参数后,添加到集合中。根据前端传过来的scene_str获取到openid。

5.根据openid查询到用户信息返回toekn登录。

三、微信公众号(手机端网页授权登录):

1.需要企业的公众号号(服务号)

2.登录公众号以后需要配置token:https://blog.csdn.net/qq_20382175/article/details/136808195?spm=1001.2014.3001.5502

3.设置白名单

4.调取微信api获取access_token

5.根据access_token获取到code再或openid,再根据openid获取微信用户信息

要在Spring Security中使用微信扫码登录,您需要完成以下步骤: 1. 在微信开放平台上创建应用程序并配置授权域名。 2. 在应用程序中创建一个扫码登录的方式,获取重定向URL和state参数。 3. 在Spring Security配置中添加一个OAuth2客户端,包括客户端ID、客户端密钥、授权域和重定向URL。 4. 创建一个自定义的OAuth2认证过滤器,用于处理微信回调请求。 以下是一个简单的示例,演示如何在Spring Security中使用微信扫码登录: ``` @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private OAuth2AuthorizedClientService authorizedClientService; @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/login/**").permitAll() .anyRequest().authenticated() .and() .oauth2Login() .redirectionEndpoint() .baseUri("/login/oauth2/code/wechat") .and() .userInfoEndpoint() .userService(wechatOAuth2UserService()) .and() .authorizedClientService(authorizedClientService) .and() .logout() .logoutUrl("/logout") .logoutSuccessUrl("/") .permitAll(); } @Bean public OAuth2UserService<OAuth2UserRequest, OAuth2User> wechatOAuth2UserService() { return new WechatOAuth2UserService(); } private static class WechatOAuth2UserService implements OAuth2UserService<OAuth2UserRequest, OAuth2User> { @Override public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException { // 获取微信用户信息 Map<String, Object> userInfo = userRequest.getAdditionalParameters(); String openid = (String) userInfo.get("openid"); String nickname = (String) userInfo.get("nickname"); String avatarUrl = (String) userInfo.get("headimgurl"); // 构造OAuth2User对象 return new DefaultOAuth2User(Collections.singleton(new SimpleGrantedAuthority("ROLE_USER")), userInfo, "openid"); } } @Bean public OAuth2AuthorizedClientRepository authorizedClientRepository() { return new HttpSessionOAuth2AuthorizedClientRepository(); } @Bean public OAuth2AuthorizedClientService authorizedClientService( OAuth2ClientRegistrationRepository clientRegistrationRepository, OAuth2AuthorizedClientRepository authorizedClientRepository) { return new DefaultOAuth2AuthorizedClientService(clientRegistrationRepository, authorizedClientRepository); } @Bean public ClientRegistrationRepository clientRegistrationRepository() { // 配置微信OAuth2客户端 ClientRegistration wechat = ClientRegistration.withRegistrationId("wechat") .clientId("your-client-id") .clientSecret("your-client-secret") .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE) .redirectUriTemplate("{baseUrl}/login/oauth2/code/{registrationId}") .scope("snsapi_login") .authorizationUri("https://open.weixin.qq.com/connect/qrconnect") .tokenUri("https://api.weixin.qq.com/sns/oauth2/access_token") .userInfoUri("https://api.weixin.qq.com/sns/userinfo") .userNameAttributeName("openid") .clientName("Wechat") .build(); return new InMemoryClientRegistrationRepository(wechat); } @Bean public OAuth2AuthorizationRequestRedirectFilter oauth2AuthorizationRequestRedirectFilter( OAuth2AuthorizedClientService authorizedClientService) { // 自定义OAuth2认证过滤器,用于处理微信回调请求 OAuth2AuthorizationRequestRedirectFilter filter = new OAuth2AuthorizationRequestRedirectFilter( clientRegistrationRepository(), authorizedClientRepository()); filter.setAuthorizationRequestRepository( new HttpSessionOAuth2AuthorizationRequestRepository()); filter.setAuthorizationUri("https://open.weixin.qq.com/connect/qrconnect"); filter.setPrincipalExtractor(new OAuth2AccessTokenResponseClientAdapter( new DefaultOAuth2AccessTokenResponseClient()).getPrincipalExtractor()); filter.setAuthorizationClientService(authorizedClientService); return filter; } } ``` 您需要替换上述代码中的`your-client-id`和`your-client-secret`为您的微信应用程序的客户端ID和客户端密钥。 此外,您还需要编写一个控制器来处理登录页面和回调URL: ``` @Controller public class LoginController { @Autowired private OAuth2AuthorizedClientService authorizedClientService; @GetMapping("/login") public String login(HttpServletRequest request, Model model) { // 创建扫码登录URL和state参数 String redirectUrl = "http://" + request.getServerName() + ":" + request.getServerPort() + "/login/oauth2/code/wechat"; String state = UUID.randomUUID().toString().replaceAll("-", ""); // 将state参数存储在会话中 request.getSession().setAttribute("state", state); // 构造微信扫码登录URL String qrCodeUrl = "https://open.weixin.qq.com/connect/qrconnect?appid=your-app-id&redirect_uri=" + URLEncoder.encode(redirectUrl, "UTF-8") + "&response_type=code&scope=snsapi_login&state=" + state + "#wechat_redirect"; model.addAttribute("qrCodeUrl", qrCodeUrl); return "login"; } @GetMapping("/login/oauth2/code/wechat") public String loginCallback(HttpServletRequest request, HttpServletResponse response, @RequestParam("code") String code, @RequestParam("state") String state, Authentication authentication) throws IOException { // 验证state参数是否匹配 String sessionState = (String) request.getSession().getAttribute("state"); if (!state.equals(sessionState)) { response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Invalid state parameter"); return null; } // 使用OAuth2AuthorizedClientService获取访问令牌 OAuth2AuthorizedClient authorizedClient = authorizedClientService.loadAuthorizedClient("wechat", authentication.getName()); String accessToken = authorizedClient.getAccessToken().getTokenValue(); // 处理用户登录逻辑 // ... return "redirect:/"; } } ``` 在`/login`请求中,我们构造了一个微信扫码登录的URL,并将其添加到模型中返回给用户。在`/login/oauth2/code/wechat`请求中,我们验证了回调请求中的state参数,并使用`OAuth2AuthorizedClientService`获取访问令牌,然后处理用户登录逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王甜甜(.NET)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值