当前位置: 首页 > 知识库问答 >
问题:

使用自定义登录表单后,OAuth2服务器不会重定向到客户端服务器

罗新
2023-03-14

身份验证和授权工作正常。但是在成功登录后,它不会将我重定向到客户端,而是进一步打开一些.js文件的源代码。而上一个(没有自定义登录表单loginPage(“/login”))它成功地将我重定向到最后一个单击的页面(客户端),该页面需要进行身份验证。

我的服务器端代码如下:

授权服务器

@Configuration

@EnableAuthorizationServer

public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

@Autowired
private AuthenticationManager authenticationManager;

@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {

    security.tokenKeyAccess("permitAll()")
            .checkTokenAccess("isAuthenticated()");
}


@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients
            .inMemory()
            .withClient("ClientId")
            .secret("secret")
            .authorizedGrantTypes("authorization_code")
            .scopes("user_info")
            .redirectUris("http://localhost:8082/ui/login")
            .autoApprove(true);
}


@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

    endpoints.authenticationManager(authenticationManager);
}
}

资源服务器

@Configuration

@EnableResourceServer

public class ResourceServer extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
    http.requestMatchers().antMatchers("/api/**").and().authorizeRequests()
            .antMatchers("/api/**").authenticated().and()
            .antMatcher("/rest/hello/principal")
            .authorizeRequests().anyRequest().authenticated();
}

我的安全配置

@Configuration

public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {


@Autowired
private CustomUserDetailsService userDetailsService;

@Override
protected void configure(HttpSecurity http) throws Exception {
        
        http.authorizeRequests().antMatchers("/login").permitAll().antMatchers("/oauth/token/revokeById/**").permitAll()
        .antMatchers("/tokens/**").permitAll().anyRequest().authenticated().and()
        .formLogin().loginPage("/login")
        .usernameParameter("username")
        .passwordParameter("password")
        .failureUrl("/login?error")
        .defaultSuccessUrl("/").permitAll().and()
        .csrf().disable();
        
}

@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userDetailsService).passwordEncoder(passwordencoder());
}

@Bean(name = "passwordEncoder")
public PasswordEncoder passwordencoder() {
    return new CustomPasswordEncoder();
}

}

共有1个答案

刘永望
2023-03-14

你需要创建 SimpleUrlAuthenticationSuccessHandler 实现

public class RefererRedirectionAuthenticationSuccessHandler 
  extends SimpleUrlAuthenticationSuccessHandler
  implements AuthenticationSuccessHandler {
 
    public RefererRedirectionAuthenticationSuccessHandler() {
        super();
        setUseReferer(true);
    }
 
}

并将一行添加到WebSecurityConfiguration.在处理程序(新的RefererReDirectionAuth在处理程序())

之后,您的方法将如下所示

@Override
protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests().antMatchers("/login").permitAll().antMatchers("/oauth/token/revokeById/**").permitAll()
        .antMatchers("/tokens/**").permitAll().anyRequest().authenticated().and()
        .formLogin().loginPage("/login")
        .usernameParameter("username")
        .passwordParameter("password")
        .successHandler(new RefererRedirectionAuthenticationSuccessHandler()) 
        .failureUrl("/login?error")
        .defaultSuccessUrl("/").permitAll().and()
        .csrf().disable();

}
 类似资料:
  • Appium 的 iOS 版本的后端用的是Facebook's WebDriverAgent。该后端是基于苹果公司的 XCTest 框架,所以也有所有XCTest 框架已知的问题。其中有些问题我们正在设法解决,有一些在现阶段可能无法解决。本文中描述的方法已经能够使您完全掌握在设备上如何构建、管理和运行WDA。通过这种方式,您可以在CI环境中对您的自动化测试进行微调,并使其在长期运行的情况下更加稳定

  • 但这样我就无法访问eureka仪表盘,只是使用默认配置: 在我的客户端中,同样的事情也发生了,我不能指向另一个不同于默认(localhost:8761)的eureka服务器,请参阅我的配置: 查看客户端日志,我得到了以下内容: 无论我在客户端配置什么端口或主机,都要尽量达到默认值。 重要提示:我在以下版本中使用eureka:https://mvnrepository.com/artifact/or

  • “入门Spring Security and Angular JS系列”中的oauth2 JWT项目有一个自定义登录名。为自定义登录页面添加相同的代码到oauth2-vanilla项目失败,因为登录响应中的授权代码始终为空。我还尝试将Sparklr2(https://github.com/spring-projects/spring-security-oauth/tree/master/sampl

  • 每个人登录后,我一直无法使用Laravel重定向。连接工作,在我登录后,它重定向到一个空白页面,但如果我更改url路径,我可以访问不同的网页。我们将不胜感激!我正在使用LDAP进行连接,它正在工作。 请让我知道,如果有任何其他代码,我应该提供。 非常感谢。 (重定向IfAuthenticated.php) 命名空间应用程序\ Http\中间件; 使用闭包;使用Illumb\Support\Faca

  • 我是Spring安全 4 的新手。我已经用支柱 2 和Spring安全性做了一个简单的应用程序。我正在使用自定义登录表单。发生的事情是当我提交登录表单时,我被重定向到 http://localhost:8080/SpringSecurity/admin 但仍然收到以下错误 HTTP状态404- /SpringSecurity/admin 类型状态报告 message/SpringSecurity/