首頁  >  文章  >  Java  >  SpringBoot怎麼實現二維碼掃碼登入

SpringBoot怎麼實現二維碼掃碼登入

PHPz
PHPz轉載
2023-05-10 20:25:111539瀏覽

一、手機掃二維碼登入的原理

二維碼掃碼登入是一種基於OAuth3.0協定的授權登入方式。在這種方式下,應用程式不需要取得使用者的使用者名稱和密碼,只需要取得使用者的授權即可。二維碼掃碼登入主要有以下步驟:

  • 應用程式產生一個二維碼,並將該二維碼展示給使用者。

  • 使用者使用掃碼工具掃描該二維碼,並在授權頁面中授權。

  • 使用者授權後,應用程式會取得一個授權碼。

  • 應用程式使用該授權碼向授權伺服器請求存取權杖。

  • 授權伺服器傳回一個存取權杖給應用程式。

  • 應用程式使用該存取權杖存取資源伺服器。

SpringBoot怎麼實現二維碼掃碼登入

透過上述步驟,二維碼掃碼登入可以實現使用者的快速登錄,並保證了使用者的安全性和隱私性。

二、SpringBoot如何實作二維碼掃碼登入

在SpringBoot中,可以使用Spring Security OAuth3.0來實作二維碼掃碼登入功能。 Spring Security OAuth3.0是一個基於OAuth3.0協定的安全性框架,它提供了授權伺服器和資源伺服器的實作。下面,我將為大家介紹如何使用Spring Security OAuth3.0實作二維碼掃碼登入。

新增依賴
首先,需要在pom.xml檔案中新增Spring Security OAuth3.0的依賴:

<dependency>
    <groupId>org.springframework.security.oauth</groupId>
    <artifactId>spring-security-oauth3</artifactId>
    <version>2.4.0</version>
</dependency>

設定授權伺服器
在SpringBoot中,可以透過@Configuration註解來配置授權伺服器。以下是一個簡單的授權伺服器設定範例:

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient("client")
                .secret("{noop}secret")
                .authorizedGrantTypes("authorization_code")
                .scopes("read", "write")
                .redirectUris("http://localhost:8080/callback");
    }

    @Override
    public void configure(AuthorizationServerEndpoints endpoints) throws Exception {
    endpoints.authenticationManager(authenticationManager);
}
}

在上面的程式碼中,使用@EnableAuthorizationServer註解來啟用授權伺服器。然後,透過@Configuration註解來指定該類別為一個配置類別。在configure()方法中,配置了一個授權客戶端,並指定了授權類型為authorization_code。授權伺服器透過inMemory()方法來指定客戶端的信息,包括客戶端ID、客戶端秘鑰、授權類型、授權範圍以及重定向地址等資訊。在configure()方法中,也需要設定AuthenticationManager,用於驗證使用者的身分資訊。

配置資源伺服器

在SpringBoot中,可以透過@Configuration註解來設定資源伺服器。下面是一個簡單的資源伺服器設定範例:

@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
  @Override
public void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .antMatchers("/api/**").authenticated()
            .anyRequest().permitAll();
}

@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
    resources.resourceId("resource");
}
}

在上面的程式碼中,使用@EnableResourceServer註解來啟用資源伺服器。然後,透過@Configuration註解來指定該類別為一個配置類別。在configure()方法中,配置了資源伺服器的安全策略,使用antMatchers()方法指定了需要認證的接口,使用permitAll()方法指定了其他接口可以被匿名訪問。在configure()方法中,也需要設定資源伺服器的資源ID。

設定客戶端

在SpringBoot中,可以透過設定檔來設定客戶端。以下是一個簡單的客戶端設定範例:

security:
oauth3:
client:
clientId: client
clientSecret: secret
accessTokenUri: http://localhost:8080/oauth/token
userAuthorizationUri: http://localhost:8080/oauth/authorize
scope: read,write
redirectUri: http://localhost:8080/callback

在上面的程式碼中,透過security.oauth3.client前綴來指定客戶端的設定訊息,包括客戶端ID、客戶端秘鑰、存取令牌URI、使用者授權URI、授權範圍、重定向位址等資訊。

產生二維碼

在SpringBoot中,可以使用第三方函式庫來產生二維碼。以下是一個簡單的二維碼產生範例:

@GetMapping("/qrcode")
public ResponseEntity<byte[]> getQRCode() throws IOException, WriterException {
String codeUrl = "http://localhost:8080/oauth/authorize?response_type=code&client_id=client&redirect_uri=http://localhost:8080/callback";
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
BitMatrix bitMatrix = new MultiFormatWriter().encode(codeUrl, BarcodeFormat.QR_CODE, 200, 200);
MatrixToImageWriter.writeToStream(bitMatrix, "png", outputStream);
return ResponseEntity.ok().contentType(MediaType.IMAGE_PNG).body(outputStream.toByteArray());
}

在上面的程式碼中,使用@GetMapping註解來指定該方法為一個GET請求處理方法,透過指定請求路徑"/qrcode"來對應該方法。在getQRCode()方法中,首先產生授權請求的URL,並使用第三方函式庫產生二維碼圖片。最後,將產生的二維碼圖片以byte數組的形式傳回給客戶端。

掃碼登入
在SpringBoot中,可以使用WebSocket來實現掃碼登入功能。以下是一個簡單的掃碼登入範例:

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(new QRCodeHandler(), "/qrcodeHandler");
    }

    class QRCodeHandler extends TextWebSocketHandler {

        private final Map<String, WebSocketSession> sessions = new ConcurrentHashMap<>();

        @Override
        public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
            String token = message.getPayload();
            if (sessions.containsKey(token)) {
                WebSocketSession clientSession = sessions.get(token);
                clientSession.sendMessage(new TextMessage("authenticated"));
                session.sendMessage(new TextMessage("authenticated"));
            } else {
                sessions.put(token, session);
            }
        }

        @Override
        public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
            sessions.values().remove(session);
        }
    }
}

在上面的程式碼中,使用@EnableWebSocket註解來啟用WebSocket支援。然後,透過@Configuration註解來指定該類別為一個配置類別。在registerWebSocketHandlers()方法中,註冊了一個WebSocket處理器,並指定了處理器的請求路徑。在QRCodeHandler類別中,實作了WebSocket處理器的業務邏輯。在handleTextMessage()方法中,將二維碼掃描後產生的token作為key,將WebSocket會話物件儲存在Map中。如果同一個token對應的WebSocket會話物件已存在,則表示該使用者已經掃碼並且已經認證通過,此時需要將兩個WebSocket會話物件互相通知認證通過。如果同一個token對應的WebSocket會話物件不存在,則將該WebSocket會話物件保存在Map中。在afterConnectionClosed()方法中,移除已關閉的WebSocket會話物件。

客戶端回呼
在SpringBoot中,可以使用@Controller註解來實現客戶端的回呼功能。下面是一個簡單的回呼範例:

@Controller
public class CallbackController {

    @Autowired
    private OAuth3RestTemplate restTemplate;

    @GetMapping("/callback")
    public String callback(@RequestParam("code") String code) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("grant_type", "authorization_code");
        params.add("code", code);
        params.add("redirect_uri", "http://localhost:8080/callback");
        params.add("client_id", "client");
        params.add("client_secret", "secret");
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(params, headers);
        OAuth3AccessToken token = restTemplate.postForObject("http://localhost:8080/oauth/token", request, OAuth3AccessToken.class);
        return "redirect:/home";
    }
}

在上面的代码中,使用@Controller注解来指定该类为一个控制器。在callback()方法中,首先使用OAuth3RestTemplate来发送POST请求获取访问令牌,并将授权码、回调URL、客户端ID和客户端秘钥等参数作为请求体发送。在获取到访问令牌之后,重定向到应用程序的主页。

以上是SpringBoot怎麼實現二維碼掃碼登入的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除