【问题标题】:Testing spring security enabled api using postman使用邮递员测试启用 Spring Security 的 API
【发布时间】:2019-11-01 19:04:09
【问题描述】:

我正在开发基于 Spring Boot + Spring Security 的应用程序。我使用 jdbcAuthentication 来验证用户。我还配置了自定义登录表单。

运行应用程序后,我能够成功登录并通过浏览器获取 API 响应,但是当我尝试使用 Postman 测试 API 时,我只得到 HTML 登录页面作为响应。如何获得所需的 API json 响应?

我的配置文件:

@Override
            protected void configure(AuthenticationManagerBuilder auth)
                    throws Exception {
            System.out.println("auth manager called");
              auth. jdbcAuthentication() .usersByUsernameQuery(usersQuery)
              .authoritiesByUsernameQuery(rolesQuery) .dataSource(dataSource)
              .passwordEncoder(noop);

            }
         @Override
            protected void configure(HttpSecurity http) throws Exception {
                System.out.println("Http scurity called");
                http.httpBasic().
                and().
                        authorizeRequests()
                        .antMatchers("/").permitAll()
                        .antMatchers("/login").permitAll()
                        .antMatchers("/registration").permitAll()
                        .antMatchers("/admin/**").hasAuthority("ADMIN")
                        .antMatchers("/db").hasAuthority("DBA")
                        .antMatchers("/user").hasAuthority("USER").anyRequest()
                        .authenticated().and().csrf().disable().formLogin()
                        .loginPage("/login").failureUrl("/login?error=true")
                        .successHandler(customSuccessHandler)
                        .usernameParameter("username")
                        .passwordParameter("password")
                        .and().logout()
                        .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                        .logoutSuccessUrl("/").and().exceptionHandling()
                        .accessDeniedPage("/access-denied");
            }

我的控制器文件:

@RequestMapping(value = { "/", "/login" }, method = RequestMethod.GET)
        public ModelAndView login() {
            System.out.println("/login called");
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("login");
            return modelAndView;
        }

        @RequestMapping(value = "/admin", method = RequestMethod.GET, produces = { "application/json" })
            public UserUniconnect home(HttpServletRequest request, HttpServletResponse response) {

                Authentication auth = SecurityContextHolder.getContext().getAuthentication();
                String currentUser = null;
                if (!(auth instanceof AnonymousAuthenticationToken)) {
                    currentUser = auth.getName();
                }

                User user1 = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
                user1.getAuthorities();
                System.out.println("++++++++++++++++++++++++++++++");
                System.out.println(request == null);
                Users u = (Users) request.getSession(false).getAttribute("user");
                Uniconnect uni = (Uniconnect) request.getSession(false).getAttribute("uniconnect");
                UserUniconnect uu = new UserUniconnect();
                uu.setUser(u);
                uu.setUniconnect(uni);

                return uu;
            }

我返回 java 对象作为 Spring Boot 能够将其转换为 json 格式的响应。

Postman Screenshot

【问题讨论】:

    标签: java spring-boot authentication postman


    【解决方案1】:

    在 Postman 中设置基本身份验证参数可能会有所帮助:

    您很可能需要在使用浏览器手动登录后从 cookie 中获取会话 id,然后像这样将该 cookie 提供给 Postman:

    从浏览器获取 cookie 取决于浏览器本身,但 Chrome 和 Firefox 都内置了 Developer utils,所以这应该不是问题。

    【讨论】:

    • 尝试使用 cookie,但我的 Post 请求仍以某种方式获取 HTML 登录页面。
    • 不知道,如果来自浏览器的 cookie 是正确的,它应该可以工作,如果它被适当地命名并且它的内容没有以某种方式搞砸。
    • 哦,但你当然使用的是 httpBasic 身份验证,我的错。 Postman 中也有一个标签!在左侧的“授权”中,选择类型:“基本身份验证”并尝试输入您的登录名和密码。也许会有所帮助。
    • 请找到附件中的图片。
    • 您希望测试来自端点“/admin”的 json 响应,但在您的屏幕截图中,您的目标 URL 是“/login”。当然,邮递员会在那里发送请求,而您将获得登录页面标记而不是 json。更改网址。以及请求类型,因为您在代码中有“RequestMethod.GET”,在屏幕截图中有 POST。它只是不会映射。
    猜你喜欢
    • 1970-01-01
    • 2017-05-19
    • 2019-05-14
    • 2017-12-17
    • 2022-07-28
    • 1970-01-01
    • 2021-03-19
    • 2021-05-12
    相关资源
    最近更新 更多