Dalam pembangunan Java API, pengesahan merupakan isu yang tidak dapat dielakkan. OAuth2 ialah kaedah pengesahan popular yang melindungi sumber API dengan memberikan akses. Artikel ini akan memperkenalkan cara menggunakan OAuth2 untuk pengesahan dalam pembangunan API Java.
Pengenalan kepada OAuth2
OAuth2 ialah standard terbuka untuk kebenaran yang membolehkan pengguna membenarkan aplikasi pihak ketiga mengakses sumber pelayan mereka tanpa perlu berkongsi bukti kelayakan mereka. Standard OAuth2 merangkumi peranan berikut:
Klien menghantar permintaan kebenaran kepada. Pelayan Kebenaran, Termasuk ID Klien dan URI hala tuju; Pelayan Kebenaran menghantar kod keizinan ke URI pengalihan ;
<dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> <version>2.3.4.RELEASE</version> </dependency>
<security:http pattern="/oauth/token" create-session="stateless" authentication-manager-ref="authenticationManager" xmlns="http://www.springframework.org/schema/security"> <security:intercept-url pattern="/oauth/token" access="isAuthenticated()" method="POST" /> <security:anonymous enabled="false" /> <security:http-basic entry-point-ref="clientAuthenticationEntryPoint" /> <security:custom-filter ref="clientCredentialsTokenEndpointFilter" before="BASIC_AUTH_FILTER" /> <security:access-denied-handler ref="oauthAccessDeniedHandler" /> </security:http> <security:http pattern="/api/**" create-session="never" entry-point-ref="oauthAuthenticationEntryPoint" access-decision-manager-ref="accessDecisionManager" xmlns="http://www.springframework.org/schema/security"> <security:anonymous enabled="false" /> <security:intercept-url pattern="/api/**" access="ROLE_USER" /> <security:custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" /> <security:access-denied-handler ref="oauthAccessDeniedHandler" /> </security:http> <bean id="clientAuthenticationEntryPoint" class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint"> <property name="realmName" value="spring-boot-oauth2" /> <property name="typeName" value="Basic" /> </bean> <bean id="oauthAuthenticationEntryPoint" class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint"> <property name="realmName" value="spring-boot-oauth2" /> <property name="typeName" value="Bearer" /> </bean> <bean id="oauthAccessDeniedHandler" class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler" /> <bean id="clientCredentialsTokenEndpointFilter" class="org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter"> <property name="authenticationManager" ref="authenticationManager" /> </bean> <bean id="resourceServerFilter" class="org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter"> <property name="authenticationManager" ref="authenticationManager" /> </bean> <bean id="accessDecisionManager" class="org.springframework.security.access.vote.UnanimousBased" xmlns="http://www.springframework.org/schema/beans"> <constructor-arg> <list> <bean class="org.springframework.security.oauth2.provider.vote.ScopeVoter" /> <bean class="org.springframework.security.access.vote.RoleVoter" /> <bean class="org.springframework.security.access.vote.AuthenticatedVoter" /> </list> </constructor-arg> </bean> <security:authentication-manager id="authenticationManager"> <security:authentication-provider user-service-ref="userDetailsService" /> </security:authentication-manager> <bean id="userDetailsService" class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl"> <property name="dataSource" ref="dataSource" /> </bean>
OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(client, context); AuthorizationCodeResourceDetails details = (AuthorizationCodeResourceDetails)client.getResource(); AuthorizationCodeAccessTokenProvider provider = new AuthorizationCodeAccessTokenProvider(); Authentication auth = new UsernamePasswordAuthenticationToken(username, password); AccessTokenRequest tokenRequest = provider.createAccessTokenRequest(details, auth); OAuth2AccessToken accessToken = provider.obtainAccessToken(details, tokenRequest); restTemplate.getOAuth2ClientContext().setAccessToken(accessToken);
Klien menghantar permintaan kepada Pelayan Kebenaran. , termasuk ID Pelanggan, Rahsia Pelanggan dan nama pengguna dan kata laluan Pemilik Sumber
Pelayan Kebenaran mengesahkan ID Pelanggan, Rahsia Pelanggan dan nama pengguna dan kata laluan, dan jika betul, mengeluarkan token akses kepada Pelanggan
Klien menggunakan Token akses menghantar permintaan kepada Pelayan Sumber.
<dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> <version>2.3.4.RELEASE</version> </dependency>
<security:http pattern="/oauth/token" create-session="stateless" authentication-manager-ref="authenticationManager" xmlns="http://www.springframework.org/schema/security"> <security:intercept-url pattern="/oauth/token" access="isAuthenticated()" method="POST" /> <security:anonymous enabled="false" /> <security:http-basic entry-point-ref="clientAuthenticationEntryPoint" /> <security:custom-filter ref="clientCredentialsTokenEndpointFilter" before="BASIC_AUTH_FILTER" /> <security:access-denied-handler ref="oauthAccessDeniedHandler" /> </security:http> <security:http pattern="/api/**" create-session="never" entry-point-ref="oauthAuthenticationEntryPoint" access-decision-manager-ref="accessDecisionManager" xmlns="http://www.springframework.org/schema/security"> <security:anonymous enabled="false" /> <security:intercept-url pattern="/api/**" access="ROLE_USER" /> <security:custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" /> <security:access-denied-handler ref="oauthAccessDeniedHandler" /> </security:http> <bean id="clientAuthenticationEntryPoint" class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint"> <property name="realmName" value="spring-boot-oauth2" /> <property name="typeName" value="Basic" /> </bean> <bean id="oauthAuthenticationEntryPoint" class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint"> <property name="realmName" value="spring-boot-oauth2" /> <property name="typeName" value="Bearer" /> </bean> <bean id="oauthAccessDeniedHandler" class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler" /> <bean id="clientCredentialsTokenEndpointFilter" class="org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter"> <property name="authenticationManager" ref="authenticationManager" /> </bean> <bean id="resourceServerFilter" class="org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter"> <property name="authenticationManager" ref="authenticationManager" /> </bean> <bean id="accessDecisionManager" class="org.springframework.security.access.vote.UnanimousBased" xmlns="http://www.springframework.org/schema/beans"> <constructor-arg> <list> <bean class="org.springframework.security.oauth2.provider.vote.ScopeVoter" /> <bean class="org.springframework.security.access.vote.RoleVoter" /> <bean class="org.springframework.security.access.vote.AuthenticatedVoter" /> </list> </constructor-arg> </bean> <security:authentication-manager id="authenticationManager"> <security:authentication-provider user-service-ref="userDetailsService" /> </security:authentication-manager> <bean id="userDetailsService" class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl"> <property name="dataSource" ref="dataSource" /> </bean>
OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(details, new DefaultOAuth2ClientContext()); restTemplate.getOAuth2ClientContext().setAccessToken(accesstoken);
Atas ialah kandungan terperinci Menggunakan pengesahan OAuth2 dalam pembangunan API Java. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!