【问题标题】:spring security not authentication from a post ethodspring security 不是来自 post 方法的身份验证
【发布时间】:2018-09-06 07:48:42
【问题描述】:

我想通过 Spring Security 中的 POST 方法对用户进行身份验证。帖子点击了控制器方法,但用户从未获得身份验证。这是场景

@Autowired
    private AuthenticationManagerBuilder builder;
    @RequestMapping(value="/signin", method = RequestMethod.POST)
    public ResponseData<Client> login(@RequestParam(value="username") String name, @RequestParam(value="password") String password,HttpServletRequest req) {

        System.out.println("here..."); //this executes

        Client ac = accountRepository.findByEmailAndActive(name,true);
        //does the authentication
        final Authentication authentication = builder.getOrBuild().authenticate(
                new UsernamePasswordAuthenticationToken(
                        name,
                        password
                )
        );
        SecurityContextHolder.getContext().setAuthentication(authentication);
        return ResponseData.successData(ac);
    }

这是我的 spring 安全方法/处理程序

.antMatchers(HttpMethod.POST,"/signin").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login").defaultSuccessUrl("/index")
                .loginProcessingUrl("/signin2")
                .permitAll()
                .and()
                .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"))

                .logoutSuccessUrl("/login?logout=true")
                .deleteCookies("JSESSIONID", "remember-me")
                .invalidateHttpSession(true)
                .and().csrf().disable()
                .rememberMe().tokenRepository(persistentTokenRepository()).tokenValiditySeconds(1200000);

请帮忙

【问题讨论】:

    标签: java spring spring-boot spring-security http-post


    【解决方案1】:

    spring security 中的默认登录路径类似于http://localhost:8080/login。按照this 说明更改此路径。如果你已经这样做了,请提供你的实现。

    【讨论】:

    • 我自己的案例已经覆盖了默认登录网址,但控制器中没有进行身份验证
    • 但是你在哪里以及如何做到这一点的。
    • 看这里>>>这个推送到上面的控制器>>>>> .antMatchers(HttpMethod.POST,"/signin").permitAll()
    • 所以当它到达那里时应该进行身份验证
    • 你理解错了@Sleek。 .antMatchers(HttpMethod.POST,"/signin").permitAll() 只允许不验证登录端点。
    【解决方案2】:

    如果您希望对 /signin 端点进行身份验证,则必须从您的安全配置中删除 .antMatchers(HttpMethod.POST,"/signin").permitAll()

    【讨论】:

    • 从我的尝试中我如何从端点执行身份验证
    • @Sleek 您在.antMatchers().permitAll() 中指定的任何端点都将被允许在没有身份验证的情况下通过。配置中的.anyRequest().authenticated() 行指定了所有其他要进行身份验证的请求。你清楚吗?
    • @Sleek 这是否回答了您的问题?如果没有,请告诉我。
    猜你喜欢
    • 2016-02-09
    • 2014-01-08
    • 2013-02-04
    • 2014-12-04
    • 2012-07-05
    • 2014-04-20
    • 2014-12-13
    • 1970-01-01
    • 2016-08-05
    相关资源
    最近更新 更多