有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java允许所有用户查看站点

这是我第一次使用Spring Security,我正在为配置而挣扎。我希望所有人都能看到/register/login,所有其他方法在登录后都应该可用。 我有一个@PostMapping的方法,它映射到/register(正如您已经知道的,您将昵称和密码作为有效负载发送)

@PostMapping("/register")
    public Account createUser(@RequestBody Account account) {
        return accountService.createAccount(account);
    }

我有一个简单的配置,它应该允许所有用户查看/注册,但它没有

我的配置:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/register", "/login").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin().permitAll();
    }

在《邮递员》中我看到:

<body>
    <div class="container">
        <form class="form-signin" method="post" action="/login">
            <h2 class="form-signin-heading">Please sign in</h2>
            <p>
                <label for="username" class="sr-only">Username</label>
                <input type="text" id="username" name="username" class="form-control" placeholder="Username" required autofocus>
        </p>
                <p>
                    <label for="password" class="sr-only">Password</label>
                    <input type="password" id="password" name="password" class="form-control" placeholder="Password" required>
        </p>
                    <input name="_csrf" type="hidden" value="72eb1e55-d2c7-455e-9046-19c7cb3c8e00" />
                    <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
        </form>
</body>

共 (1) 个答案

  1. # 1 楼答案

    我不得不补充一点

    @Override
        public void configure(WebSecurity web) throws Exception {
            web
                    .ignoring()
                    .antMatchers(HttpMethod.POST, "/register");
        }