I am trying to create a registration page. Even though I am submitting the form, the data isn’t being inserted in the postgresql user_table. Logging in works if I insert the data into the table manually. I’m unsure what I’m doing wrong.
register.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Sign Up</title>
<link href="webjars/bootstrap/5.1.0/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container col-xl-10 col-xxl-8 px-4 py-5">
<div class="row align-items-center g-lg-5 py-5">
<div class="col-md-10 mx-auto col-lg-5">
<form class="p-4 p-md-5 border rounded-3 bg-light" th:action="@{/process_register}" th:object="${user}" method="post">
<div class="form-floating mb-3">
<input type="text" class="form-control" id="floatingInput" placeholder="UserName" th:field="*{username}">
<label for="floatingInput">User Name</label>
</div>
<div class="form-floating mb-3">
<input type="text" class="form-control" id="floatingInput" placeholder="FirstName" th:field="*{firstName}">
<label for="floatingInput">First Name</label>
</div>
<div class="form-floating mb-3">
<input type="text" class="form-control" id="floatingInput" placeholder="LastName" th:field="*{lastName}">
<label for="floatingInput">Last Name</label>
</div>
<div class="form-floating mb-3">
<input type="email" class="form-control" id="floatingInput" placeholder="[email protected]" th:field="*{email}">
<label for="floatingInput">Email address</label>
</div>
<div class="form-floating mb-3">
<input type="password" class="form-control" id="floatingPassword" placeholder="Password" th:field="*{password}">
<label for="floatingPassword">Password</label>
</div>
<button class="w-100 btn btn-lg btn-info" type="submit">Sign Up</button>
<hr class="my-4">
<small class="text-muted">If you already have an account, <a href="/">log in</a>.</small>
</form>
</div>
</div>
</div>
<script src="webjars/jquery/3.6.0/jquery.min.js"></script>
<script src="webjars/bootstrap/5.1.0/js/bootstrap.min.js"></script>
<script>
</script>
</body>
</html>
TwitterController.java
package com.example.demo;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@Controller
public class TwitterController{
private UserRepository userRepo;
@RequestMapping("/")
public String index(Model model) {
//model.addAttribute("message", "Welcome to spring!");
return "index";
}
@RequestMapping("/testing")
public String testing(Model model) {
model.addAttribute("message", "Welcome to spring boot!");
return "testing";
}
@RequestMapping("/register")
public String signup(Model model) {
User user = new User();
model.addAttribute("user", user);
return "register";
}
@RequestMapping("/login")
public String login(Model model) {
return "login";
}
@PostMapping("/process_register")
public String processRegister(User user) {
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
String encodedPassword = encoder.encode(user.getPassword());
user.setPassword(encodedPassword);
userRepo.save(user);
return "register_success";
}
User.java
package com.example.demo;
import java.util.Collection;
@Entity
@Table(name = "user_table")
public class User implements UserDetails{
private static final long serialVersionUID = 1L;
@Id
private Integer id;
private String username;
private String password;
private String email;
private String fname;
private String lname;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return fname;
}
public void setFirstName(String fname) {
this.fname = fname;
}
public String getLastName() {
return lname;
}
public void setLastName(String lname) {
this.lname = lname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + "fname=" + fname + "lname=" + lname + "email=" + email + "]";
}
@Override
public Set<GrantedAuthority> getAuthorities() {
Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
authorities.add(new SimpleGrantedAuthority("USER"));
return authorities;
}
}
UserRepository.java
package com.example.demo;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Integer>{
public User findByUsername(String username);
}
UserDetailsService.java
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
@Service
public class UserDetailsService implements org.springframework.security.core.userdetails.UserDetailsService{
@Autowired
private UserRepository repository;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = repository.findByUsername(username);
if (user == null) {
throw new UsernameNotFoundException(username);
}
return user;
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Twitter-clone</name>
<description>Making a twitter clone</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2
Answers
SecurityConfiguration is blocking the HTTPRequest. The other problem that I was having afterwards was that insertion still was not working. The user_table did not have an auto-increment id, and thus returned null and could not be inserted like that. Solution was either to change the hibernation settings or update user_table to auto-increment for the id.
the annotation @Repository is missing in the interface UserRepository, also you should autowire this dependency by adding the annotation @Autowired in the field of dependency declaration in class TwitterController