404页面

wfsdck30  于 2021-09-30  发布在  Java
关注(0)|答案(1)|浏览(212)

我是Spring的新手,我正在尝试实现一个自定义登录页面。我可以重定向到自定义登录页面,但当我点击页面中的提交按钮时,会出现错误404。url转到:http://localhost:12345/login?username=d&password=qwer
以下是htm、控制器和安全配置的代码:
securityconfig.java

http
    .authorizeRequests()
    .antMatchers("/main",
            "/main#/home",
            "/login**",
            "/resources/**",
            "/css/**",
            "/images/**",
            "/js/**",
            "/std/**",
            "/system/**",
            "/favicon.ico",
            "/rest/**",
            "/rest/user/**" ).permitAll()
    .anyRequest().authenticated()
    .and().csrf().disable()
    .formLogin()
    .loginPage("/main#/login").permitAll()
    .defaultSuccessUrl("/main#/home")
    .failureUrl("/main#/page/b").permitAll();

logincontroller.java

@RestController
@RequestMapping(value = "/rest/user")
public class LoginController 
{

    @RequestMapping(value = "login")
    public String login(Model model, String error, String logout) {
        if (error != null)
            model.addAttribute("errorMsg", "Your username and password are invalid.");

        if (logout != null)
            model.addAttribute("msg", "You have been logged out successfully.");

        return "login";
    }
}

login-content.htm

<div class="col-md-6 col-md-offset-3">
<h2>Login</h2>
<form name="form" action="/login" ng-controller="LoginController" role="form">

    <div class="form-group" ng-class="{ 'has-error': form.username.$dirty && form.username.$error.required }">
        <label for="username">Username</label>
        <input type="text" name="username" id="username" class="form-control" ng-model="user.username" required />
        <span ng-show="form.username.$dirty && form.username.$error.required" class="help-block">Username is required</span>
    </div>
    <div class="form-group" ng-class="{ 'has-error': form.password.$dirty && form.password.$error.required }">
        <label for="password">Password</label>
        <input type="password" name="password" id="password" class="form-control" ng-model="user.password" required />
        <span ng-show="form.password.$dirty && form.password.$error.required" class="help-block">password is required</span>
    </div>

    <input type="submit" ng-click="login(user)" value="Submit">
</form>

谢谢

ndasle7k

ndasle7k1#

最后,我将login-content.htm中的“action”修改为另一个单词(例如perform\u login),并在securityconfig.java中添加了.loginprocessingurl(“/perform\u login”).permitall()。
此外,另一个问题是Spring Security 需要有一个特定的post方法才能对其进行身份验证。我在htm页面中添加了method=“post”。
谢谢

相关问题