请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 null

集成资源服务调用认证服务出现异常信息如下:org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 null

图片描述

图片描述

后台打印异常信息:

org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 null
at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:81) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:122) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:102) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
at org.springframework.security.oauth2.provider.token.RemoteTokenServices$1.handleError(RemoteTokenServices.jav

请问有人知道我哪里写错了吗?贴代码如下

认证服务代码:
/**
*
*/
package com.imooc.security.server.auth;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;

/**

  • @author jojo

*/

@Configuration
@EnableAuthorizationServer
public class OAuth2AuthServerConfig extends AuthorizationServerConfigurerAdapter {

@Autowired
private AuthenticationManager authenticationManager;

@Autowired
private DataSource dataSource;

@Autowired
private PasswordEncoder passwordEncoder;

@Autowired
private UserDetailsService userDetailsService;

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

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

	 clients.inMemory() 
	  .withClient("orderApp")
	  .secret(passwordEncoder.encode("123456")) 
	  .scopes("read","write")
	  .accessTokenValiditySeconds(3600)
	  .resourceIds("order-server")
	  .authorizedGrantTypes("password")
	  .and() 
	  .withClient("orderService")
	  .secret(passwordEncoder.encode("123456")) 
	  .scopes("read","write")
	  .accessTokenValiditySeconds(3600) 
	  .resourceIds("order-server")
	  .authorizedGrantTypes("password", "authorization_code", "refresh_token") ;
	  
}

@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
	security  .tokenKeyAccess("isAuthenticated()").checkTokenAccess("permitAll()") ;
}

}

资源服务代码:

package com.mice.server.resource;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;

@Configuration
@EnableResourceServer
public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter {

@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception { // 生命自己的id
																						
	resources.resourceId("order-server");

}

/*
 * @Override public void configure(HttpSecurity http) throws Exception {
 * //那些不需要授权,在这里配置
 * http.authorizeRequests().antMatchers("/haha").permitAll().anyRequest().
 * authenticated(); }
 */

}

package com.mice.server.resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationManager;
import org.springframework.security.oauth2.provider.token.RemoteTokenServices;
import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices;

@Configuration

@EnableWebSecurity
public class Ouath2WebSecurityConfig extends WebSecurityConfigurerAdapter {
/*
* @Autowired private PasswordEncoder passwordEncoder;
*/

@Bean
public ResourceServerTokenServices tokenServices() {
	RemoteTokenServices tokenServices = new RemoteTokenServices();
	tokenServices.setClientId("orderService");
	tokenServices.setClientSecret("123456");
	tokenServices.setCheckTokenEndpointUrl("http://localhost:9090/ouath/check_token");
	return tokenServices;
}

@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
	OAuth2AuthenticationManager auth2AuthenticationManager = new OAuth2AuthenticationManager();
	auth2AuthenticationManager.setTokenServices(tokenServices());
	return auth2AuthenticationManager;

}

}

正在回答 回答被采纳积分+3

3回答

慕粉3333946 2021-03-30 19:27:47

一样的问题,解决了吗

0 回复 有任何疑惑可以回复我~
慕用7359371 2020-08-02 12:06:29

一样的问题,解决了吗?

0 回复 有任何疑惑可以回复我~
我是可愛蛋蛋 2020-07-17 10:04:52

把方法改成这个试试

@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
   security.checkTokenAccess("permitAll()


");
}

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号