首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >页,如果登录表单自定义表单成功

页,如果登录表单自定义表单成功
EN

Stack Overflow用户
提问于 2022-11-01 17:01:46
回答 1查看 29关注 0票数 -1

我是春天保安的新手。在角度上,我有一个自定义表单,它向这个控制器发送一个post:

代码语言:javascript
复制
@RestController
public class LoginController {
    @PostMapping("/login")
    public ResponseEntity<String> login(@RequestBody User user) {
        if (user.getUsername().equals("linda") && user.getPassword().equals("pass")) {
            return ResponseEntity.ok("Access granted");
        }
        return ResponseEntity.badRequest().body("Access denied");
    }
}

如果登录成功,用户应该可以访问页面/welcome,否则该页面将被禁止。

这就是我尝试过的:

代码语言:javascript
复制
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class Config {
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder(10);
    }

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                .antMatchers("/login").permitAll()
                .antMatchers("/welcome").hasAnyRole("ADMIN")
                .anyRequest()
                .authenticated()
                .and()
                .formLogin(form -> form.loginPage("/login").permitAll());
        return http.build();
    }

    @Bean
    public InMemoryUserDetailsManager userDetailsManager() {
        var linda = User.builder()
                .username("linda")
                .password(passwordEncoder().encode("pass"))
                .roles("ADMIN")
                .build();
        return new InMemoryUserDetailsManager(linda);
    }
}

当我调用/login时,我得到了不允许的状态405 -方法。

可能我什么都做错了。如何才能正确地做到这一点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-01 19:10:04

在您的登录端点中应该更新两件事:

  1. @PostMapping("login")应该是@PostMapping("/login")
  2. 您应该将包含用户名和密码的类定义为字段,并将端点参数更新为以下内容: public ResponseEntity<String> login(@RequestBody CustomUser customUser) { 或者您可以如下所示使用它:public ResponseEntity<String> login(@RequestParam(value = "username") String username, @RequestParam(value = "password") String password) {

更新:

代码语言:javascript
复制
Following your update, i've resimulate your scenario and the configuration is wrong as you've already mentioned.

配置应该如下所示,不需要创建登录端点:

代码语言:javascript
复制
@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder(10);
}

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    http.csrf().disable().authorizeRequests().antMatchers("/login", "/loginFailed").permitAll()
            .antMatchers("/welcome").hasAnyRole("ADMIN").anyRequest()
            .authenticated().and()
            .formLogin(form -> form.loginPage("/login")
                    .usernameParameter("username")
                    .passwordParameter("password")
                    .defaultSuccessUrl("/welcome")
                    .failureUrl("/loginFailed"));
    return http.build();
}

@Bean
public InMemoryUserDetailsManager userDetailsManager() {
    UserDetails linda = User.builder().username("linda")
            .password(passwordEncoder().encode("pass")).roles("ADMIN")
            .build();
    return new InMemoryUserDetailsManager(linda);
}

但是,用户名和密码应该按照"UsernamePasswordAuthenticationFilter“中的默认配置在查询param中提交。您可以通过实现自定义UsernamePasswordAuthenticationFilter来更改默认行为。(检查)

端点:

代码语言:javascript
复制
@GetMapping("/welcome")
public ResponseEntity<String> welcome(Authentication authentication) {
    return ResponseEntity.ok("Weclome [" + authentication.getName() + "]");
}

@GetMapping("/loginFailed")
public ResponseEntity<String> login() {
    return ResponseEntity.ok("Failed to login");
}

希望它能帮上忙

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74279334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档