首页 > 解决方案 > OAUTH2:成功表单登录后没有出现授权决策页面

问题描述

我正在为获取授权代码编写基于表单的 OAUTH 身份验证。在资源服务器要求资源所有者进行身份验证以及授权共享数据之后,它应该有一个“授权决策页面”。

以下是服务器端配置

授权服务器

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
     @Bean
        public BCryptPasswordEncoder passwordEncoder() {
            return new BCryptPasswordEncoder();
        }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory().withClient("javainuse").secret("{noop}secret").authorizedGrantTypes("authorization_code")
            .scopes("read").authorities("CLIENT");
    }
}

网络安全配置器

@Configuration
@EnableWebSecurity
public class EmployeeSecurityConfiguration extends WebSecurityConfigurerAdapter {

     @Bean
        @Override
        public AuthenticationManager authenticationManager() throws Exception {

            return super.authenticationManager();
        }

     @Autowired
     private BCryptPasswordEncoder  passwordEncoder;



    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/resources/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/user/getEmployeesList")
            .hasAnyRole("ADMIN").anyRequest().authenticated().and().formLogin()
            .permitAll().and().logout().permitAll();

        http.csrf().disable();
    }

    @Override
    public void configure(AuthenticationManagerBuilder authenticationMgr) throws Exception {
        authenticationMgr.inMemoryAuthentication().withUser("admin").password(passwordEncoder.encode("admin"))
            .authorities("ROLE_ADMIN");
    }


}

以下是客户端配置

控制器

@Controller
public class EmployeeController {

    @RequestMapping(value = "/getEmployees", method = RequestMethod.GET)
    public ModelAndView getEmployeeInfo() {
        return new ModelAndView("getEmployees");
    }

    @RequestMapping(value = "/showEmployees", method = RequestMethod.GET)
    public String getEmployeeInfo1() {
        return "showEmployees";
    }
}

获取雇员.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add Employee</title>
</head>
<body>
    <h3 style="color: red;">Add New Employee</h3>

    <div id="addEmployee">
        <form:form action="http://localhost:8081/oauth/authorize"
            method="post" modelAttribute="emp">
            <p>
                <label>Enter Employee Id</label>
                 <input type="text" name="response_type" value="code" /> 
                 <input type="text" name="client_id" value="javainuse" />
                 <input type="text" name="redirect_uri" value="http://localhost:8090/showEmployees" />
                 <input type="text" name="scope" value="read" /> 
                 <input type="SUBMIT" value="Get Employee info" />
        </form:form>
    </div>
</body>
</html>

在登录提示上提供登录详细信息后

在此处输入图像描述

我提供了详细信息,在它应该在http://localhost:8081/oauth/authorize向我提供提示之后

在此处输入图像描述

它给了我关于日志的信息

INFO AuthorizationEndpoint : Handling OAuth2 error: error="invalid_request", error_description="At least one redirect_uri must be registered with the client."

非常感谢任何帮助,不知道我在哪里做错了。我正在使用 Spring Boot 2.0.2.RELEASE。

标签: spring-bootoauth-2.0

解决方案


输入标签中给出的redirect_uri值http://localhost:8090/showEmployees也需要在上面的授权服务器配置中映射,所以

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients.inMemory().withClient("javainuse").secret("secret").authorizedGrantTypes("authorization_code")
        .scopes("read").authorities("CLIENT").redirectUris("http://localhost:8090/showEmployees");
}

推荐阅读