我是Spring Boot的新手。我正在尝试从“注册”表单获取用户信息,然后将其保存到数据库中。但是,当我在注册页面上填写详细信息时,它会重定向到登录/注册页面,并且不会将任何数据添加到数据库,而不会出现错误和日志。 以下是HTML代码:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head lang="en">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>xyzSite</title>
<!-- Bootstrap -->
<link href="css/bootstrap.css" rel="stylesheet">
<link href="css/custom-styling.css" rel="stylesheet">
<link href="css/FormStyling.css" rel="stylesheet">
<!-- Favicons -->
<link href="img/logo.png" rel="icon">
<link href="img/logo.png" rel="ResumeFlexer_Icon">
</head>
<body>
<div id="wrapper">
<nav class="navbar navbar-inverse" style="margin-bottom: -20px;">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">RESUME FLEXER</a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav navbar-right">
<li><a href="/">Home</a></li>
<li><a href="/#about">About</a></li>
<li><a href="recent">Recent Post</a></li>
<li><a href="faq">FAQ</a></li>
<li><a href="donate">Donate</a></li>
<li class="active"><a href="login"><span class="glyphicon glyphicon-user"></span> Sign Up / Login</a></li>
</ul>
</div>
</div>
</nav>
<div class="parallax_full">
<div class="container">
<div class="row">
<!-- ################################################### -->
<!-- LogIn Form -->
<div class="col-xs-12 login-style">
<h1>Login</h1>
<div th:if="${param.error}" class="alert alert-danger">
<label style="color:red">Invalid username and Password</label>
</div>
<div th:if="${param.logout}" class="alert alert-success">
<label>You have been logged out</label>
</div>
<form name="f" th:action="@{/login}" method="post">
<div class="form-group col-lg-6">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" name="username" placeholder="Username" required/>
</div>
<div class="form-group col-lg-6">
<label for="pssword">Password</label>
<input type="password" class="form-control" id="password" name="password" placeholder="Password" required/>
</div>
<div class="form-group" style="text-align:center;">
<input type="submit" id="submit" class="btn btn-primary btn-md" value="Log In"/>
</div>
</form>
</div> <!-- Login div end -->
<hr>
<!-- ################################################### -->
<!-- Sign Up Form -->
<section id="SignUp">
<div class="col-xs-12 SignUp">
<h1>Not a Member? <br>Create Your Account!</h1>
<p>It's Free !</p>
<form action="adduser" method="POST">
<div class="form-group">
<label for="name"> Full Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Ex: Steves Jobs" required/>
</div>
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" name="username" placeholder="Username" required/>
</div>
<div class="form-group">
<label for="emailId">Email Id</label>
<input type="mail Id" class="form-control" id="emailId" name="emailId"
placeholder="Email" required/>
</div>
<div class="form-group">
<label for="phone_no">Phone No.</label>
<input type="text" class="form-control" id="phone_no" name="phone_no" placeholder="Phone Number" required/>
</div>
<div class="form-group">
<label for="dob">Date Of Birth</label>
<input type="date" class="form-control dobDate" name="dob" id="dob"/>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" name ="password" placeholder="Password" required/>
</div>
<label>Gender</label>
<div class="radio">
<label class="radio-inline"><input type="radio" name="gender" checked>Male</label>
<label class="radio-inline"><input type="radio" name="gender">Female</label>
<label class="radio-inline"><input type="radio" name="gender">Other</label>
</div>
<div class="form-group">
<label for="field">Field</label>
<input type="field" class="form-control" id="field" name="field"
placeholder="Software Engineering or Data Science, etc."/>
</div>
<div class="form-group">
<label for="nationality">Nationality</label>
<input type="nationality" class="form-control" id="nationality" name="nationality"
placeholder="Indian / English / American"/>
</div>
<div class="form-group">
<nobr><input type="checkbox" id="termsAndCondition" name="termsAndCondition" value="agreed" required><a href="/termsAndCondition" style="color:#fff;"> Terms and Conditions</a></nobr>
</div>
<div class="form-group">
<input type="submit" id="submit" class="btn btn-primary btn-lg" value="Create Account"/>
</div>
</form>
</div>
</section>
</row>
</div>
</div>
</div> <!-- Wrapper div ending -->
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src = "js/customLoginValidation.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
</body>
控制器映射注册表单
@RequestMapping(method=RequestMethod.POST,value="/adduser")
public String addUser(@RequestParam("name") String name,
@RequestParam("username") String username,
@RequestParam("emailId") String email,
@RequestParam("phone_no") String phone_no,
@RequestParam("dob") @DateTimeFormat(pattern="yyyy-MM-dd") Date dob,
@RequestParam("password") String password,
@RequestParam("gender") String gender,
@RequestParam("field") String field,
@RequestParam("nationality") String nationality,
@RequestParam("termsAndCondition") String termsAndCondition) {
System.out.println("addUser() method called");
User user = new User(username,name,password,email,phone_no,field,dob,gender,nationality,new Date());
userservice.addUser(user);
return "user/profile";
}
安全配置
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().requireCsrfProtectionMatcher(new AntPathRequestMatcher("**/login"))
.and()
.authorizeRequests()
.antMatchers("/profile").hasAnyRole("USER", "ADMIN")
.antMatchers("admin/**").hasRole("ADMIN")
.antMatchers("user/**").hasRole("ADMIN")
.antMatchers("/","/faq","/donate","/recent","/signup").permitAll()
.antMatchers("/h2/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.failureUrl("/login?error=true")
.successHandler(successHandler())
.defaultSuccessUrl("/user/profile")
.permitAll()
.and()
.logout()
.logoutSuccessUrl("/login?logout")
.permitAll();
}
答案 0 :(得分:3)
您正在{
"script_fields": {
"adas": {
"script": {
"inline": "doc['field_one.keyword'].value == doc['field_two.keyword'].value",
"lang": "painless"
}
}
}
上提交表单。但这未经授权,因此您可能会收到/adduser
401状态代码。您还可以像下面这样在“安全配置”中授权HTTP.Unauthorized
路由吗?
/adduser
答案 1 :(得分:0)
该表单已过帐到/adduser
。据我所知,安全配置中不允许使用路径/adduser
。尝试添加
.authorizeRequests()
.antMatchers("/adduser/**").permitAll()
答案 2 :(得分:0)
似乎要求/adduser
正在执行在用户应用程序中添加用户的任务。尝试在代码库中做两件事。
/adduser
设置一个条目。 .authorizeRequests()
.antMatchers("/adduser/**").permitAll()
user/profile
。 if(userservice.addUser(user)){
return "user/profile";
}