Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring引导登录返回401,并带有匹配的凭据_Java_Spring Boot_Spring Security_Spring Security Oauth2 - Fatal编程技术网

Java Spring引导登录返回401,并带有匹配的凭据

Java Spring引导登录返回401,并带有匹配的凭据,java,spring-boot,spring-security,spring-security-oauth2,Java,Spring Boot,Spring Security,Spring Security Oauth2,我有一个SpringBoot应用程序,可以登录和注册新用户。我的用户保存在数据库中(到目前为止,我使用内存H2数据库进行测试) 该服务由OAuth2保护(它是我正在开发的其他服务的身份验证服务器) 一切正常,我可以检查用户的凭据,但我想在成功登录后将用户重定向到某个地方。如果我使用response\u type=token参数访问登录页面,它就会工作 http://localhost:9000/auth/oauth/authorize?response_type=token&client

我有一个SpringBoot应用程序,可以登录和注册新用户。我的用户保存在数据库中(到目前为止,我使用内存H2数据库进行测试)

该服务由OAuth2保护(它是我正在开发的其他服务的身份验证服务器)

一切正常,我可以检查用户的凭据,但我想在成功登录后将用户重定向到某个地方。如果我使用
response\u type=token
参数访问登录页面,它就会工作

http://localhost:9000/auth/oauth/authorize?response_type=token&client_id=client&redirect_uri=http://localhost:9000/auth/user
但是,当我直接访问登录页面并登录时,它会将我重定向到我选择作为默认成功页面的页面,但没有令牌或任何其他指示用户已登录并获得401的信息。直接进入
/login
并使用正确的凭据,结果如下:

<oauth>
  <error_description>
    Full authentication is required to access this resource
  </error_description>
  <error>unauthorized</error>
</oauth>
OAuth2Config:

@Configuration
@EnableAuthorizationServer
public class OAuth2Config extends AuthorizationServerConfigurerAdapter {

    @Autowired
    @Qualifier("userDetailsService")
    private UserDetailsService userDetailsService;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private TokenStore tokenStore;

    @Autowired
    private DataSource dataSource;

    // password encryptor
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer configurer) throws Exception {
        configurer.authenticationManager(authenticationManager);
        configurer.userDetailsService(userDetailsService);
        configurer.tokenStore(tokenStore);

    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.jdbc(dataSource);
    }

}
登录控制器:

@Controller
public class LoginController {

    @GetMapping("/login")
    public String login() {
        return "login";
    }
}
登录页面:

<body>

    <div class="container">

        <form class="form-signin" th:action="@{/login}" method="post">
            <h2 class="form-signin-heading">Sign in</h2>

            <div th:if="${param.error}">Invalid username and password.</div>
            <div th:if="${param.logout}">You have been logged out.</div>

            <label for="inputEmail" class="sr-only">Email</label> 
            <input type="text" id="username"
                class="form-control" placeholder="Email" name="username" th:required="required" th:autofocus="autofocus" />
            <label for="inputPassword" class="sr-only">Password</label>

            <input type="password" id="inputPassword"
                class="form-control" placeholder="Password" name="password" th:required="required" />

            <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
        </form>

    </div>
    <!-- /container -->

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>

登录
无效的用户名和密码。
您已注销。
电子邮件
密码
登录
谢谢你的帮助。如果这还不够,我很乐意提供更多细节

编辑:我想要的基本上是身份验证服务器和其他微服务之间的无柄身份验证,但基于身份验证服务器本身登录的会话。

首先检查这一点:

如果使用基本表单登录创建访问令牌,我将添加自定义身份验证成功处理程序:

Web安全配置:

@Autowired
private AuthenticationSuccessHandler myAuthenticationSuccessHandler;

// 

.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/")
.successHandler(myAuthenticationSuccessHandler)
以及处理程序“MySimpleUrlAuthenticationSuccessHandler”:


这是一种可能性。但是也有可能举行一些会议吗?那么令牌将用于外部客户端,会话将在表单登录后在auth服务器上举行?所有其他的例子都是开箱即用的,所以我不确定我做错了什么…看看这是否有帮助:我不认为这篇文章与我的问题有关。请你提供一个成功处理者应该是什么样的例子?所以修改后的url不会返回401。嘿,你在运行两台服务器吗?像验证服务器在端口9000上运行,资源服务器在端口8080上运行之类的?
@Autowired
private AuthenticationSuccessHandler myAuthenticationSuccessHandler;

// 

.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/")
.successHandler(myAuthenticationSuccessHandler)
@Component("myAuthenticationSuccessHandler")
public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    //logic to grant access
}