Jan*_*an 4 java oauth-2.0 spring-boot
我有2个正在运行的应用程序,一个是资源服务器,其中有我需要身份验证才能查看文本的信息。然后,我有提供令牌的授权服务器。现在,我可以使用邮递员或失眠者,添加auth_url,token_url,client_id,client_secret,然后我会得到令牌。我将令牌添加到标头中,并使用标头向资源服务器执行get请求,并且它工作得很好。
现在我不知道如何直接从资源服务器实现重定向。就像我去
本地主机:9000 / home
我想重定向到:
本地主机:9001 /登录
我使用内存用户登录的位置,然后将我重定向回localhost:9000 / home,然后看到消息。
实现用户访问localhost:9000 / home上的信息的最佳方法是什么?您转到localhost:9000 / home,然后转到localhost:9001上的授权服务器,并使用用户名和密码登录。批准授予,它使您返回到localhost:9000 / home,然后您可以看到文本,该文本以前受到保护,因为您没有令牌来访问它。
ResourceServer.java
@SpringBootApplication
@RestController
@EnableResourceServer
@EnableOAuth2Client
public class SampleResourceApplication extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.antMatcher("/**")
.authorizeRequests()
.antMatchers("/", "/login**").hasRole("user")
.anyRequest().authenticated();
}
@Bean
public RequestContextListener requestContextListener() {
return new RequestContextListener();
}
public static void main(String[] args) {
SpringApplication.run(SampleResourceApplication.class, args);
}
@RequestMapping("/home")
public String home() {
return "this is home";
}
}
Run Code Online (Sandbox Code Playgroud)
和我的财产看起来像:
server:
port: 900
security:
oauth2:
client:
client-id: foo
client-secret: foosecret
access-token-uri: http://localhost:9001/auth/oauth/token
user-authorization-uri: http://localhost:9001/auth/oauth/authorize
grant-type: USER
auto-approve-scopes: true
resource:
user-info-uri: http://localhost:9001/auth/user
Run Code Online (Sandbox Code Playgroud)
让我们分离代理:您拥有用户(即您,也称为资源所有者),授权服务器,资源服务器和客户端(访问您的URL的应用程序,即浏览器)。
通常,这发生在您的情况下:
401。根据您的实现,您还可以直接将客户端重定向到您的AS(使用简单的重定向响应)。您尝试获得的(如果我理解正确的话)是使用令牌自动重定向。为此,您只需在步骤1的末尾localhost:9000/home将重定向到AS时传递尝试访问的URL(即)。AS hten会提示用户输入凭据,生成令牌,并将其存储为cookie(在(如果是浏览器),则将您重定向到他收到的网址(localhost:9000/home)。
编辑:重定向的结果代码是什么。
进入时configure,您首先要检查用户是否已通过身份验证。如果他是,那么一切都很好,但是如果他不是,则必须捕获此事件并开始重定向。这可以使用exceptionHandling链接的方法来完成http:
public void configure(HttpSecurity http) throws Exception {
http.antMatcher("/**")
.authorizeRequests()
.antMatchers("/", "/login**").hasRole("user")
.anyRequest().authenticated()
.and()
.exceptionHandling()
.authenticationEntryPoint(authenticationEntryPoint());
}
private AuthenticationEntryPoint authenticationEntryPoint() {
return new AuthenticationEntryPoint() {
// You can use a lambda here
@Override
public void commence(HttpServletRequest aRequest, HttpServletResponse aResponse,
AuthenticationException aAuthException) throws IOException, ServletException {
aResponse.sendRedirect(MY_AS_URL + "?redirect_uri=localhost:9001/home");
}
};
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7099 次 |
| 最近记录: |