我想将google oayut2添加到我的客户端crud应用程序中。 与服务器的交互使用resttemplate进行。 我在Google控制台中注册了一个项目,在应用程序中添加了@ EnableOAuth2Sso,属性甚至是过滤器(尽管没有它似乎也是可能的)
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
beginSearch("Hello")
}
private fun beginSearch(srsearch: String) {
val call = realiApiService.searchArtist("query", "json", "search", srsearch)
call.enqueue(object : Callback<Any> {
override fun onResponse(call: Call<Any>, response: Response<Any>) {
Log.d("response is ${response.body()}")
}
override fun onFailure(call: Call<Any>, t: Throwable) {
Log.d("Throwable is $t")
}
})
//myApiService.executeCall(call)
}
}
google属性:
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private OAuth2ClientContext oauth2ClientContext;
private AuthorizationCodeResourceDetails authorizationCodeResourceDetails;
private ResourceServerProperties resourceServerProperties;
@Autowired
public void setOauth2ClientContext(OAuth2ClientContext oauth2ClientContext) {
this.oauth2ClientContext = oauth2ClientContext;
}
@Autowired
public void setResourceServerProperties(ResourceServerProperties resourceServerProperties) {
this.resourceServerProperties = resourceServerProperties;
}
@Autowired
private CustomAuthenticationSuccessHandler customAuthenticationSuccessHandler;
@Bean
public RestTemplate rest() {
return new RestTemplate();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
filter.setForceEncoding(true);
http
.authorizeRequests()
.antMatchers("/user/**").hasAnyAuthority("USER")
.antMatchers("/admin/**").hasAnyAuthority("ADMIN")
.and()
.formLogin()
.loginPage("/")
.failureUrl("/?error")
.successHandler(customAuthenticationSuccessHandler)
.usernameParameter("username")
.passwordParameter("password")
.and()
// Setting the filter for the URL "/google/login".
.addFilterBefore(filter(), BasicAuthenticationFilter.class)
.csrf().disable();
}
@Bean
public static NoOpPasswordEncoder passwordEncoder() {
return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
}
/*This method for creating filter for OAuth authentication.*/
private OAuth2ClientAuthenticationProcessingFilter filter() {
//Creating the filter for "/google/login" url
OAuth2ClientAuthenticationProcessingFilter oAuth2Filter = new OAuth2ClientAuthenticationProcessingFilter(
"/login/google");
//Creating the rest template for getting connected with OAuth service.
//The configuration parameters will inject while creating the bean.
OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(authorizationCodeResourceDetails,
oauth2ClientContext);
oAuth2Filter.setRestTemplate(oAuth2RestTemplate);
// Setting the token service. It will help for getting the token and
// user details from the OAuth Service.
oAuth2Filter.setTokenServices(new UserInfoTokenServices(resourceServerProperties.getUserInfoUri(),
resourceServerProperties.getClientId()));
return oAuth2Filter;
}
}
在html中:
spring.security.oauth2.client.registration.google.client-id=170427894383-
io56an8cpvrp9ucj301vo06qb9m67fgn.apps.googleusercontent.com
spring.security.oauth2.client.registration.google.client-secret=lk8OH3po6-
MXTjQ0zE_Aqv1x
spring.security.oauth2.client.registration.google.scope=profile,email,openid
spring.security.oauth2.client.registration.google.client-authentication-
method=form
spring.security.oauth2.client.provider.google.authorization-
uri=https://accounts.google.com/o/oauth2/auth
spring.security.oauth2.client.provider.google.token-
uri=https://www.googleapis.com/oauth2/v4/token
spring.security.oauth2.client.provider.google.user-info-
uri=https://www.googleapis.com/oauth2/v3/userinfo
spring.security.oauth2.client.provider.google.user-name-attribute=sub
当尝试登录时,401 null立即下降。 没有帮助