Issue
this is the error message that appeared after clicking on the submit button on the register page
Wed Aug 10 14:48:00 CET 2022 There was an unexpected error (type=Internal Server Error, status=500). rawPassword cannot be null
java.lang.IllegalArgumentException: rawPassword cannot be null
at org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder.encode(BCryptPasswordEncoder.java:107)
at almanara.almanaraplateforme.web_signin_signup.Controller.LoginController.registerPost(LoginController.java:42)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1070)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:681)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:327)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:115)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:81)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:122)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:116)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:126)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:81)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:109)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:149)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:223)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:217)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:223)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:217)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
at org.springframework.security.oauth2.client.web.OAuth2AuthorizationRequestRedirectFilter.doFilterInternal(OAuth2AuthorizationRequestRedirectFilter.java:178)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:103)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:89)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
at org.springframework.security.web.header.HeaderWriterFilter.doHeadersAfter(HeaderWriterFilter.java:90)
at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:75)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:112)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:82)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:55)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
at org.springframework.security.web.session.DisableEncodeUrlFilter.doFilterInternal(DisableEncodeUrlFilter.java:42)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:211)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:183)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:354)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:267)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:890)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1789)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:833)
**
> this is the user entity that i used for the register form here you can find the attributes getters and setters etc...
**
@Entity
@Table(name = "utilisateurs")
public class Utilisateur {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idUtl;
@NotBlank(message = "le nom est obligatoire")
private String nomUtl;
@NotBlank(message = "le prenom est obligatoire")
private String prenomUlt;
@NotEmpty
@Length(min = 8, message = "le mot de passe doit contenir au minimum 8 chracteres")
private String passwordUtl;
//@NotBlank(message = "le numero telephone est obligatoire")
@Column(nullable = false, unique = true)
private Long telUtl;
@Column(nullable = false, unique = true)
@NotEmpty
@Email(message = "{erros.invalid_email}")
private String email;
private String adresseUtl;
private String emploiUtl;
@NotBlank(message = "la region est obligatoire")
private String regionUtl;
@NotBlank(message = "le secteru d'activite est obligatoire")
private String secteuractiviteUtl;
//private boolean isAgriculteur = false;
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.MERGE)
@JoinTable(
name = "utilisateurs_roles",
joinColumns = @JoinColumn(
name = "utilisateurs_id", referencedColumnName = "idUtl"),
inverseJoinColumns = @JoinColumn(
name = "role_id", referencedColumnName = "id"))
private List<Role> roles;
public Set<Produit> getProduits() {
return produits;
}
public void setProduits(Set<Produit> produits) {
this.produits = produits;
}
@JsonIgnore
@OneToMany(mappedBy = "utilisateur")
private Set<Produit> produits = new HashSet<>();
public Utilisateur() {super();}
public Utilisateur(
String nomUtl,
String prenomUlt,
@NotEmpty String passwordUtl,
Long telUtl,
@NotEmpty @Email(message = "{erros.invalid_email}") String email,
String adresseUtl,
String emploiUtl,
String regionUtl,
String secteuractiviteUtl,
List<Role> roles) {
this.nomUtl = nomUtl;
this.prenomUlt = prenomUlt;
this.passwordUtl = passwordUtl;
this.telUtl = telUtl;
this.email = email;
this.adresseUtl = adresseUtl;
this.emploiUtl = emploiUtl;
this.regionUtl = regionUtl;
this.secteuractiviteUtl = secteuractiviteUtl;
this.roles = roles;
}
public Utilisateur(Utilisateur utilisateur) {
this.nomUtl = utilisateur.getNomUtl();
this.prenomUlt = utilisateur.getPrenomUlt();
this.passwordUtl = utilisateur.getPasswordUtl();
this.telUtl = utilisateur.getTelUtl();
this.email = utilisateur.getEmail();
this.adresseUtl = utilisateur.getAdresseUtl();
this.emploiUtl = utilisateur.getEmploiUtl();
this.regionUtl = utilisateur.getRegionUtl();
this.secteuractiviteUtl = utilisateur.getSecteuractiviteUtl();
this.roles = utilisateur.getRoles();
}
public Utilisateur(String nomUtl,
String prenomUlt,
@NotEmpty String passwordUtl,
Long telUtl,
@NotEmpty @Email String email,
String adresseUtl,
String emploiUtl,
String regionUtl,
String secteuractiviteUtl) {
this.nomUtl = nomUtl;
this.prenomUlt = prenomUlt;
this.passwordUtl = passwordUtl;
this.telUtl = telUtl;
this.email = email;
this.adresseUtl = adresseUtl;
this.emploiUtl = emploiUtl;
this.regionUtl = regionUtl;
this.secteuractiviteUtl = secteuractiviteUtl;
}
public String getPassword() {
return passwordUtl;
}
public String getUsername() {
return email;
}
public boolean isAgriculteur() {
return isAgriculteur;
}
public void setAgriculteur(boolean agriculteur) {
isAgriculteur = agriculteur;
}
public Long getIdUtl() {
return idUtl;
}
public void setIdUtl(Long idUtl) {
this.idUtl = idUtl;
}
public String getNomUtl() {
return nomUtl;
}
public void setNomUtl(String nomUtl) {
this.nomUtl = nomUtl;
}
public List<Role> getRoles() {
return roles;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
public String getPrenomUlt() {
return prenomUlt;
}
public void setPrenomUlt(String prenomUlt) {
this.prenomUlt = prenomUlt;
}
public String getPasswordUtl() {
return passwordUtl;
}
public void setPasswordUtl(String passwordUtl) {
this.passwordUtl = passwordUtl;
}
public Long getTelUtl() {
return telUtl;
}
public void setTelUtl(Long telUtl) {
this.telUtl = telUtl;
}
public String getEmail() {
return email;
}
public void setEmail(String emailUtl) {
this.email = emailUtl;
}
public String getAdresseUtl() {
return adresseUtl;
}
public void setAdresseUtl(String addresseUtl) {
this.adresseUtl = addresseUtl;
}
public String getEmploiUtl() {
return emploiUtl;
}
public void setEmploiUtl(String emploiUtl) {
this.emploiUtl = emploiUtl;
}
public String getRegionUtl() {
return regionUtl;
}
public void setRegionUtl(String regionUtl) {
this.regionUtl = regionUtl;
}
public String getSecteuractiviteUtl() {
return secteuractiviteUtl;
}
public void setSecteuractiviteUtl(String secteuractiviteUtl) {
this.secteuractiviteUtl = secteuractiviteUtl;
}
```
this the login controller for the register and the login here are the POST and GET
```java
@Controller
public class LoginController {
@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
@Autowired
UtilisateurRepo utilisateurRepo;
@Autowired
RoleRepo roleRepo;
@GetMapping("/login")
public String login(){
GlobalData.cart.clear();
return "login";
}
@GetMapping("/register")
public String registerGet(){
return "register";
}
@PostMapping("/register")
public String registerPost(@ModelAttribute("Utilisateur") Utilisateur Utilisateur, HttpServletRequest request)throws ServletException{
String password=Utilisateur.getPassword();
Utilisateur.setPasswordUtl(bCryptPasswordEncoder.encode(password));
List<Role> roles = new ArrayList<>();
roles.add(roleRepo.findById(2L).get());
Utilisateur.setRoles(roles);
utilisateurRepo.save(Utilisateur);
request.login(Utilisateur.getEmail(), password);
return "redirect:/";
}
}
this is the html code for the login page. I did not made this i just found it on the internet and tried to change it so it can matches my user entity however i think the error is here but i can not find it usually the thobject so it matches my enity and i also modify the thfield but as i said i couldnt know what to change. I am an html newbie tho.
<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css"
integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous">
<title>Registration Form</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light" >
<div class="container-fluid">
<a class="navbar-brand" href="#">
<img th:src="@{/images/logo.png}" src="../static/images/logo.png" width="auto" height="40" class="d-inline-block align-top" alt=""/>
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto"></ul>
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" th:href="@{/}" href="#">home</a>
</li>
<li class="nav-item active">
<a class="nav-link" th:href="@{/shop}" href="#">shop</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="#">Sign Up</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="col-sm-6">
<h3 style="margin-top: 10px">Sign Up Now</h3>
<p>Please fill out this to register</p>
<!--
@thymesVar id="utilisateur" type="almanara.almanaraplateforme.Utilisateur.Utilisateur"
-->
<form th:action="@{/register}" th:object="${utilisateur}" method="post">
<div class="form-group">
<label for="firstName">First Name</label>
<input type="text" name="firstName" id="firstName" required placeholder="Your Firstname" class="form-control form-control-lg">
</div>
<div class="form-group">
<label for="lastName">Last Name</label>
<input type="text" name="lastName" id="lastName" required placeholder="Your Lastname" class="form-control form-control-lg">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control form-control-lg" required placeholder="Password" name="password"
id="password">
</div>
<div class="form-group">
<label for="telephone">telephone</label>
<input type="number" name="lastName" id="telephone" required placeholder="telephone" class="form-control form-control-lg">
</div>
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control form-control-lg" required minlength="6" placeholder="Email" name="email" id="email"
aria-describedby="emailHelp">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with
anyone else.</small>
</div>
<div class="form-group">
<label for="adresse">adresse</label>
<input type="text" name="adresse" id="adresse" required placeholder="adresse" class="form-control form-control-lg">
</div>
<div class="form-group">
<label for="emploi">emploi</label>
<input type="text" name="emploi" id="emploi" required placeholder="emploi" class="form-control form-control-lg">
</div>
<div class="form-group">
<label for="region">region</label>
<input type="text" name="region" id="region" required placeholder="region" class="form-control form-control-lg">
</div>
<div class="form-group">
<label for="secteur">secteur</label>
<input type="text" name="lastName" id="secteur" required placeholder="secteur" class="form-control form-control-lg">
</div>
<input type="submit" value="Register" class="btn btn-primary btn-block">
<span style="margin-top: 10px">Already have an account <a class="linkControl" href="./login.html"
th:href="@{/login}">Login here</a></span>
</form>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
</body>
</html>
Solution
As per your error stacktrace
java.lang.IllegalArgumentException: rawPassword cannot be null
at org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder.encode(BCryptPasswordEncoder.java:107)
it simply means your password variable which you are passing to bCryptPasswordEncoder.encode(password)
is coming as blank.
Utilisateur.setPasswordUtl(bCryptPasswordEncoder.encode(password));
and the only reason it will be blank after your forms are getting submitted from frontend because of
- You have not mapped your html attributes correctly with you Model attributes.
and hence you are getting null
for those attributes and you are passing null to the encoder and its throwing error.
Correct your thymleaf html template as per below suggestions .
Change the html as below to map the details in your ModelAttribute
correctly.
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control form-control-lg" required placeholder="Password"
name="passwordUtl"
id="password">
</div>
You are defining the html tags as name="password"
whereas it should be as per your attributes defined in entity class name="passwordUtl"
.
Your entity class have diffrent named variables and you are using diffrent name in your html.
Correct all other html names
as suggested above as per your entity class attribute names.
Answered By - Rishal
Answer Checked By - Katrina (JavaFixing Volunteer)