我有一个分发单个页面应用程序的客户服务。单页应用程序的所有请求都通过使用代理(伪装)的客户端服务来重定向呼叫。
我想允许匿名呼叫,但是我无法使用当前配置进行此操作。
为了简化起见,我提供三种服务:客户端,oauth2服务器和oauth2资源服务器。oauth2服务器也是资源服务器。
客户端使用此配置连接到oauth2-server
security:
oauth2:
client:
clientId: autorisation_code_client
clientSecret: *******
accessTokenUri: https://localhost:****/oauth2-server/oauth/token
userAuthorizationUri: https://localhost:****/oauth2-server/oauth/authorize
#tokenCheckUri: https://localhost:****/oauth2-server/oauth/check_token
resource:
userInfoUri: https://localhost:****/oauth2-server/me
这是客户端的WebSecurityConfigurerAdapter类,当用户尝试访问登录路径时,他已重定向到oauth2-server进行身份验证。
@Override
public void configure(HttpSecurity http) throws Exception {
http.antMatcher("/**")
.authorizeRequests()
.antMatchers(
"/",
"/index.html",
"/login**",
"/logout**",
//resources
"/assets/**",
"/static/**",
"/*.ico",
"/*.js",
"/*.json").permitAll()
.anyRequest()
.authenticated()
.and()
.csrf().csrfTokenRepository(csrfTokenRepository())
.and()
.addFilterAfter(csrfHeaderFilter(), SessionManagementFilter.class);
}
客户端使用的伪代理,我想将oauth2-server / user / like / ***路径配置为匿名用户可以访问。
@RestController
@FeignClient(name = "oauth2-server", url = "https://localhost:****")
public interface ProxyOauth2Server {
@GetMapping(value = "oauth2-server/user/like/{name}")
ResponseEntity<?> getUserLikeName(@PathVariable("name") String name);
}
要通过Feign传输令牌,我在客户端Main类中具有此配置。
@EnableConfigurationProperties
@SpringBootApplication
@EnableFeignClients("com.tutosharing.client.proxies")
public class ClientUiApplication {
@Autowired
private SecurityPropertiesConfig config;
@Bean
protected OAuth2ProtectedResourceDetails resource() {
AuthorizationCodeResourceDetails resource = new AuthorizationCodeResourceDetails();
resource.setAccessTokenUri(config.getAccessTokenUri());
resource.setUserAuthorizationUri(config.getUserAuthorizationUri());
resource.setClientId(config.getClientId());
resource.setClientSecret(config.getClientSecret());
return resource;
}
@Bean
public RequestInterceptor oauth2FeignRequestInterceptor(OAuth2ClientContext oauth2ClientContext,
OAuth2ProtectedResourceDetails resource) {
return new OAuth2FeignRequestInterceptor(oauth2ClientContext, resource);
}
}
现在也可以用作资源服务器的oauth2服务器
@SpringBootApplication
@EnableResourceServer
@EnableAuthorizationServer
@EnableConfigurationProperties
public class AuthorizationServerApplication {}
oauth2服务器WebSecurityConfigurerAdapter类
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.requestMatchers()
.antMatchers("/",
"/login",
"/login.do",
"/oauth/authorize**")
.and()
.authorizeRequests()
.antMatchers(
"/",
"/login",
"/login.do")
.permitAll()
.and()
.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/login.do")
.usernameParameter("*********")
.passwordParameter("*********")
.and()
.userDetailsService(userDetailsServiceBean())
.requiresChannel()
.anyRequest()
.requiresSecure();
}
}
我想允许匿名用户使用的Rest控件方法
@RestController
public class UserRControllerRest {
@GetMapping({"/user/like/{name}"})
@JsonView(View.SimpleUser.class)
@PreAuthorize("hasRole('ROLE_USER')")
public ResponseEntity getUserLikeName(@PathVariable String name) {
Set<AuthUser> users = this.userRepository.findByNameLike(name);
return new ResponseEntity(users, HttpStatus.OK);
}
}
如果我使用@PreAuthorize(“ hasRole('ROLE_ANONYMOUS')”)配置Rest方法,则>
和类似的WebSecurityConfigurerAdapter
http.requestMatchers() .antMatchers( ... "/user/like/**", ...) .and() .authorizeRequests() .antMatchers("/user/like/**") .anonymous() ... } } // @formatter:on
如果我直接与Postman联系oauth2-server,我将获得答案,但是如果我通过使用Feign的客户端服务,则无法获得答案,我总是被重定向到登录页面。
所以我如何允许通过Feign进行匿名请求?
我有一个分发单个页面应用程序的客户服务。来自单页应用程序的所有请求都通过使用代理(伪装)的客户端服务来重定向呼叫。我想要...
我已经找到了解决方案,但是我不确定这是最好的方法。因此,如果您有其他解决方案,欢迎您。