Why do I have a login issue after updating rows in my database?
up vote
1
down vote
favorite
I have a user in my database and I would like to edit their information. I am trying to add their gender and height. The problem is, that after updating my user, I am unable to log in again. This error has been quite persisting and it always happens after updating my user.
This is my HTML code:
<form th:action="@{/editprofile/save}" th:object="${user}" method="post">
And my Controller code:
@PostMapping("/editprofile/save")
public String save(@Valid User user, BindingResult result, RedirectAttributes redirect, Principal principal) {
if(result.hasErrors()) {
return "views/success";
}
System.out.println(user.getEmail());
System.out.println(user.getPassword());
System.out.println(user.getRepassword());
userService.save(user);
redirect.addFlashAttribute("success", "Saved employee successfully!");
return "redirect:/editprofile";
}
css spring spring-boot thymeleaf
add a comment |
up vote
1
down vote
favorite
I have a user in my database and I would like to edit their information. I am trying to add their gender and height. The problem is, that after updating my user, I am unable to log in again. This error has been quite persisting and it always happens after updating my user.
This is my HTML code:
<form th:action="@{/editprofile/save}" th:object="${user}" method="post">
And my Controller code:
@PostMapping("/editprofile/save")
public String save(@Valid User user, BindingResult result, RedirectAttributes redirect, Principal principal) {
if(result.hasErrors()) {
return "views/success";
}
System.out.println(user.getEmail());
System.out.println(user.getPassword());
System.out.println(user.getRepassword());
userService.save(user);
redirect.addFlashAttribute("success", "Saved employee successfully!");
return "redirect:/editprofile";
}
css spring spring-boot thymeleaf
Could you add your whole html form and your model's code?
– Alain Cruz
Nov 21 at 17:11
Show yoursavemethod of theUserService.
– M. Deinum
Nov 21 at 17:44
I added full code below
– Krzysztof Sawicki
Nov 21 at 22:27
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a user in my database and I would like to edit their information. I am trying to add their gender and height. The problem is, that after updating my user, I am unable to log in again. This error has been quite persisting and it always happens after updating my user.
This is my HTML code:
<form th:action="@{/editprofile/save}" th:object="${user}" method="post">
And my Controller code:
@PostMapping("/editprofile/save")
public String save(@Valid User user, BindingResult result, RedirectAttributes redirect, Principal principal) {
if(result.hasErrors()) {
return "views/success";
}
System.out.println(user.getEmail());
System.out.println(user.getPassword());
System.out.println(user.getRepassword());
userService.save(user);
redirect.addFlashAttribute("success", "Saved employee successfully!");
return "redirect:/editprofile";
}
css spring spring-boot thymeleaf
I have a user in my database and I would like to edit their information. I am trying to add their gender and height. The problem is, that after updating my user, I am unable to log in again. This error has been quite persisting and it always happens after updating my user.
This is my HTML code:
<form th:action="@{/editprofile/save}" th:object="${user}" method="post">
And my Controller code:
@PostMapping("/editprofile/save")
public String save(@Valid User user, BindingResult result, RedirectAttributes redirect, Principal principal) {
if(result.hasErrors()) {
return "views/success";
}
System.out.println(user.getEmail());
System.out.println(user.getPassword());
System.out.println(user.getRepassword());
userService.save(user);
redirect.addFlashAttribute("success", "Saved employee successfully!");
return "redirect:/editprofile";
}
css spring spring-boot thymeleaf
css spring spring-boot thymeleaf
edited Nov 22 at 0:24
Michael Petch
24.8k55699
24.8k55699
asked Nov 21 at 16:54
Krzysztof Sawicki
63
63
Could you add your whole html form and your model's code?
– Alain Cruz
Nov 21 at 17:11
Show yoursavemethod of theUserService.
– M. Deinum
Nov 21 at 17:44
I added full code below
– Krzysztof Sawicki
Nov 21 at 22:27
add a comment |
Could you add your whole html form and your model's code?
– Alain Cruz
Nov 21 at 17:11
Show yoursavemethod of theUserService.
– M. Deinum
Nov 21 at 17:44
I added full code below
– Krzysztof Sawicki
Nov 21 at 22:27
Could you add your whole html form and your model's code?
– Alain Cruz
Nov 21 at 17:11
Could you add your whole html form and your model's code?
– Alain Cruz
Nov 21 at 17:11
Show your
save method of the UserService.– M. Deinum
Nov 21 at 17:44
Show your
save method of the UserService.– M. Deinum
Nov 21 at 17:44
I added full code below
– Krzysztof Sawicki
Nov 21 at 22:27
I added full code below
– Krzysztof Sawicki
Nov 21 at 22:27
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
Yes this is all form :
<form th:action="@{/editprofile/save}" th:object="${user}" method="post">
<div class="form-group row">
<label for="name"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Imię: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text" th:field="*{name}" id="name">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('name')}"-->
<!--th:errors="*{name}"></div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Nazwisko: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{surname}" id="surname">
<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"
th:errors="*{surname}"></div>
</div>
</div>
<div class="form-group row">
<label for="email" class="col-sm-2 pr-0 pl-0 text-center col-form-label">Adres
Email: </label>
<div class="col-sm-10 cl-input">
<input type="email" class="form-control form-text " th:field="*{email}" id="email">
<div class="text col-sm-12 error" th:if="${#fields.hasErrors('email')}"
th:errors="*{email}"></div>
</div>
</div>
<div class="form-group row">
<label for="password"
class="col-sm-2 pr-0 pl-0 text-center hidden col-form-label">Hasło: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " value="elko" th:value="${user.getPassword()}" th:name="password" id="password">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('password')}"-->
<!--th:errors="*{password}">-->
<!--</div>-->
</div>
</div>
<div class="form-group row">
<label for="repassword" class="col-sm-2 pr-0 pl-0 text-center col-form-label">Powtórz
Hasło: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:value="${user.getRepassword()}" th:name="repassword" id="repassword">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('repassword')}"-->
<!--th:errors="*{repassword}">-->
<!--</div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Płec: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{gender}" id="gender">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"-->
<!--th:errors="*{surname}"></div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Data urodzenia: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{birthdate}" id="birthdate">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"-->
<!--th:errors="*{surname}"></div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Waga: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{weight}" id="weight">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"-->
<!--th:errors="*{surname}"></div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Wzrost: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{growth}" id="growth">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"-->
<!--th:errors="*{surname}"></div>-->
</div>
</div>
<div class="col-lg-12 ">
<div class="row">
<div class="col-lg-2"></div>
<input type="submit" value="Zapisz" class="col-lg-10 btn btn-primary"/>
</div>
</div>
</form>
And thsi is my controller save :
@GetMapping("/editprofile")
public String editeProfile( Model model, Principal principal) {
String email = principal.getName();
User user = userService.findOne(email);
model.addAttribute("user", user);
return "views/editprofile";
}
@GetMapping("/editprofile/{email}")
public String editProfile( @PathVariable String email, Model model,Principal principal) {
model.addAttribute("user", userService.findOne(email));
return "views/editprofile";
}
@PostMapping("/editprofile/save")
public String save(@Valid User user, BindingResult result, RedirectAttributes redirect) {
if(result.hasErrors()) {
return "views/success";
}
System.out.println(user.getEmail());
System.out.println(user.getPassword());
System.out.println(user.getRepassword());
userService.save(user);
redirect.addFlashAttribute("success", "Saved employee successfully!");
return "redirect:/editprofile";
}
Userservice :
public void save(User user) {
userRepository.save(user);
}
add a comment |
up vote
0
down vote
Thanks for following the approach i guided on
https://stackoverflow.com/a/53371025/10232467
Authentication and Authorization process is not defined in your given source code. We should use Spring Security to do this job. Please import spring security dependencies and make Security config class as below
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
// .loginPage("/login") in case if custom login page is required
.permitAll()
.and()
.logout()
.permitAll()
// add ant matchers and require secure in case if certain url has to be on https
;
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
// for in memory authentication
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.passwordEncoder(passwordEncoder())
.withUser("user")
.password(passwordEncoder().encode("password"))
.roles("USER");
}
}
You can get logged in User details anytime in controller by below code
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String name = auth.getName(); //get logged in username
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417014%2fwhy-do-i-have-a-login-issue-after-updating-rows-in-my-database%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Yes this is all form :
<form th:action="@{/editprofile/save}" th:object="${user}" method="post">
<div class="form-group row">
<label for="name"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Imię: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text" th:field="*{name}" id="name">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('name')}"-->
<!--th:errors="*{name}"></div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Nazwisko: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{surname}" id="surname">
<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"
th:errors="*{surname}"></div>
</div>
</div>
<div class="form-group row">
<label for="email" class="col-sm-2 pr-0 pl-0 text-center col-form-label">Adres
Email: </label>
<div class="col-sm-10 cl-input">
<input type="email" class="form-control form-text " th:field="*{email}" id="email">
<div class="text col-sm-12 error" th:if="${#fields.hasErrors('email')}"
th:errors="*{email}"></div>
</div>
</div>
<div class="form-group row">
<label for="password"
class="col-sm-2 pr-0 pl-0 text-center hidden col-form-label">Hasło: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " value="elko" th:value="${user.getPassword()}" th:name="password" id="password">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('password')}"-->
<!--th:errors="*{password}">-->
<!--</div>-->
</div>
</div>
<div class="form-group row">
<label for="repassword" class="col-sm-2 pr-0 pl-0 text-center col-form-label">Powtórz
Hasło: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:value="${user.getRepassword()}" th:name="repassword" id="repassword">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('repassword')}"-->
<!--th:errors="*{repassword}">-->
<!--</div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Płec: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{gender}" id="gender">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"-->
<!--th:errors="*{surname}"></div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Data urodzenia: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{birthdate}" id="birthdate">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"-->
<!--th:errors="*{surname}"></div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Waga: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{weight}" id="weight">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"-->
<!--th:errors="*{surname}"></div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Wzrost: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{growth}" id="growth">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"-->
<!--th:errors="*{surname}"></div>-->
</div>
</div>
<div class="col-lg-12 ">
<div class="row">
<div class="col-lg-2"></div>
<input type="submit" value="Zapisz" class="col-lg-10 btn btn-primary"/>
</div>
</div>
</form>
And thsi is my controller save :
@GetMapping("/editprofile")
public String editeProfile( Model model, Principal principal) {
String email = principal.getName();
User user = userService.findOne(email);
model.addAttribute("user", user);
return "views/editprofile";
}
@GetMapping("/editprofile/{email}")
public String editProfile( @PathVariable String email, Model model,Principal principal) {
model.addAttribute("user", userService.findOne(email));
return "views/editprofile";
}
@PostMapping("/editprofile/save")
public String save(@Valid User user, BindingResult result, RedirectAttributes redirect) {
if(result.hasErrors()) {
return "views/success";
}
System.out.println(user.getEmail());
System.out.println(user.getPassword());
System.out.println(user.getRepassword());
userService.save(user);
redirect.addFlashAttribute("success", "Saved employee successfully!");
return "redirect:/editprofile";
}
Userservice :
public void save(User user) {
userRepository.save(user);
}
add a comment |
up vote
0
down vote
Yes this is all form :
<form th:action="@{/editprofile/save}" th:object="${user}" method="post">
<div class="form-group row">
<label for="name"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Imię: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text" th:field="*{name}" id="name">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('name')}"-->
<!--th:errors="*{name}"></div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Nazwisko: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{surname}" id="surname">
<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"
th:errors="*{surname}"></div>
</div>
</div>
<div class="form-group row">
<label for="email" class="col-sm-2 pr-0 pl-0 text-center col-form-label">Adres
Email: </label>
<div class="col-sm-10 cl-input">
<input type="email" class="form-control form-text " th:field="*{email}" id="email">
<div class="text col-sm-12 error" th:if="${#fields.hasErrors('email')}"
th:errors="*{email}"></div>
</div>
</div>
<div class="form-group row">
<label for="password"
class="col-sm-2 pr-0 pl-0 text-center hidden col-form-label">Hasło: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " value="elko" th:value="${user.getPassword()}" th:name="password" id="password">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('password')}"-->
<!--th:errors="*{password}">-->
<!--</div>-->
</div>
</div>
<div class="form-group row">
<label for="repassword" class="col-sm-2 pr-0 pl-0 text-center col-form-label">Powtórz
Hasło: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:value="${user.getRepassword()}" th:name="repassword" id="repassword">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('repassword')}"-->
<!--th:errors="*{repassword}">-->
<!--</div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Płec: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{gender}" id="gender">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"-->
<!--th:errors="*{surname}"></div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Data urodzenia: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{birthdate}" id="birthdate">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"-->
<!--th:errors="*{surname}"></div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Waga: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{weight}" id="weight">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"-->
<!--th:errors="*{surname}"></div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Wzrost: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{growth}" id="growth">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"-->
<!--th:errors="*{surname}"></div>-->
</div>
</div>
<div class="col-lg-12 ">
<div class="row">
<div class="col-lg-2"></div>
<input type="submit" value="Zapisz" class="col-lg-10 btn btn-primary"/>
</div>
</div>
</form>
And thsi is my controller save :
@GetMapping("/editprofile")
public String editeProfile( Model model, Principal principal) {
String email = principal.getName();
User user = userService.findOne(email);
model.addAttribute("user", user);
return "views/editprofile";
}
@GetMapping("/editprofile/{email}")
public String editProfile( @PathVariable String email, Model model,Principal principal) {
model.addAttribute("user", userService.findOne(email));
return "views/editprofile";
}
@PostMapping("/editprofile/save")
public String save(@Valid User user, BindingResult result, RedirectAttributes redirect) {
if(result.hasErrors()) {
return "views/success";
}
System.out.println(user.getEmail());
System.out.println(user.getPassword());
System.out.println(user.getRepassword());
userService.save(user);
redirect.addFlashAttribute("success", "Saved employee successfully!");
return "redirect:/editprofile";
}
Userservice :
public void save(User user) {
userRepository.save(user);
}
add a comment |
up vote
0
down vote
up vote
0
down vote
Yes this is all form :
<form th:action="@{/editprofile/save}" th:object="${user}" method="post">
<div class="form-group row">
<label for="name"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Imię: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text" th:field="*{name}" id="name">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('name')}"-->
<!--th:errors="*{name}"></div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Nazwisko: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{surname}" id="surname">
<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"
th:errors="*{surname}"></div>
</div>
</div>
<div class="form-group row">
<label for="email" class="col-sm-2 pr-0 pl-0 text-center col-form-label">Adres
Email: </label>
<div class="col-sm-10 cl-input">
<input type="email" class="form-control form-text " th:field="*{email}" id="email">
<div class="text col-sm-12 error" th:if="${#fields.hasErrors('email')}"
th:errors="*{email}"></div>
</div>
</div>
<div class="form-group row">
<label for="password"
class="col-sm-2 pr-0 pl-0 text-center hidden col-form-label">Hasło: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " value="elko" th:value="${user.getPassword()}" th:name="password" id="password">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('password')}"-->
<!--th:errors="*{password}">-->
<!--</div>-->
</div>
</div>
<div class="form-group row">
<label for="repassword" class="col-sm-2 pr-0 pl-0 text-center col-form-label">Powtórz
Hasło: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:value="${user.getRepassword()}" th:name="repassword" id="repassword">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('repassword')}"-->
<!--th:errors="*{repassword}">-->
<!--</div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Płec: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{gender}" id="gender">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"-->
<!--th:errors="*{surname}"></div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Data urodzenia: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{birthdate}" id="birthdate">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"-->
<!--th:errors="*{surname}"></div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Waga: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{weight}" id="weight">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"-->
<!--th:errors="*{surname}"></div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Wzrost: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{growth}" id="growth">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"-->
<!--th:errors="*{surname}"></div>-->
</div>
</div>
<div class="col-lg-12 ">
<div class="row">
<div class="col-lg-2"></div>
<input type="submit" value="Zapisz" class="col-lg-10 btn btn-primary"/>
</div>
</div>
</form>
And thsi is my controller save :
@GetMapping("/editprofile")
public String editeProfile( Model model, Principal principal) {
String email = principal.getName();
User user = userService.findOne(email);
model.addAttribute("user", user);
return "views/editprofile";
}
@GetMapping("/editprofile/{email}")
public String editProfile( @PathVariable String email, Model model,Principal principal) {
model.addAttribute("user", userService.findOne(email));
return "views/editprofile";
}
@PostMapping("/editprofile/save")
public String save(@Valid User user, BindingResult result, RedirectAttributes redirect) {
if(result.hasErrors()) {
return "views/success";
}
System.out.println(user.getEmail());
System.out.println(user.getPassword());
System.out.println(user.getRepassword());
userService.save(user);
redirect.addFlashAttribute("success", "Saved employee successfully!");
return "redirect:/editprofile";
}
Userservice :
public void save(User user) {
userRepository.save(user);
}
Yes this is all form :
<form th:action="@{/editprofile/save}" th:object="${user}" method="post">
<div class="form-group row">
<label for="name"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Imię: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text" th:field="*{name}" id="name">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('name')}"-->
<!--th:errors="*{name}"></div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Nazwisko: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{surname}" id="surname">
<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"
th:errors="*{surname}"></div>
</div>
</div>
<div class="form-group row">
<label for="email" class="col-sm-2 pr-0 pl-0 text-center col-form-label">Adres
Email: </label>
<div class="col-sm-10 cl-input">
<input type="email" class="form-control form-text " th:field="*{email}" id="email">
<div class="text col-sm-12 error" th:if="${#fields.hasErrors('email')}"
th:errors="*{email}"></div>
</div>
</div>
<div class="form-group row">
<label for="password"
class="col-sm-2 pr-0 pl-0 text-center hidden col-form-label">Hasło: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " value="elko" th:value="${user.getPassword()}" th:name="password" id="password">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('password')}"-->
<!--th:errors="*{password}">-->
<!--</div>-->
</div>
</div>
<div class="form-group row">
<label for="repassword" class="col-sm-2 pr-0 pl-0 text-center col-form-label">Powtórz
Hasło: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:value="${user.getRepassword()}" th:name="repassword" id="repassword">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('repassword')}"-->
<!--th:errors="*{repassword}">-->
<!--</div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Płec: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{gender}" id="gender">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"-->
<!--th:errors="*{surname}"></div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Data urodzenia: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{birthdate}" id="birthdate">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"-->
<!--th:errors="*{surname}"></div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Waga: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{weight}" id="weight">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"-->
<!--th:errors="*{surname}"></div>-->
</div>
</div>
<div class="form-group row">
<label for="surname"
class="col-sm-2 pr-0 pl-0 text-center col-form-label">Wzrost: </label>
<div class="col-sm-10 cl-input">
<input type="text" class="form-control form-text " th:field="*{growth}" id="growth">
<!--<div class="text col-sm-12 error" th:if="${#fields.hasErrors('surname')}"-->
<!--th:errors="*{surname}"></div>-->
</div>
</div>
<div class="col-lg-12 ">
<div class="row">
<div class="col-lg-2"></div>
<input type="submit" value="Zapisz" class="col-lg-10 btn btn-primary"/>
</div>
</div>
</form>
And thsi is my controller save :
@GetMapping("/editprofile")
public String editeProfile( Model model, Principal principal) {
String email = principal.getName();
User user = userService.findOne(email);
model.addAttribute("user", user);
return "views/editprofile";
}
@GetMapping("/editprofile/{email}")
public String editProfile( @PathVariable String email, Model model,Principal principal) {
model.addAttribute("user", userService.findOne(email));
return "views/editprofile";
}
@PostMapping("/editprofile/save")
public String save(@Valid User user, BindingResult result, RedirectAttributes redirect) {
if(result.hasErrors()) {
return "views/success";
}
System.out.println(user.getEmail());
System.out.println(user.getPassword());
System.out.println(user.getRepassword());
userService.save(user);
redirect.addFlashAttribute("success", "Saved employee successfully!");
return "redirect:/editprofile";
}
Userservice :
public void save(User user) {
userRepository.save(user);
}
edited Nov 21 at 22:47
answered Nov 21 at 22:26
Krzysztof Sawicki
63
63
add a comment |
add a comment |
up vote
0
down vote
Thanks for following the approach i guided on
https://stackoverflow.com/a/53371025/10232467
Authentication and Authorization process is not defined in your given source code. We should use Spring Security to do this job. Please import spring security dependencies and make Security config class as below
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
// .loginPage("/login") in case if custom login page is required
.permitAll()
.and()
.logout()
.permitAll()
// add ant matchers and require secure in case if certain url has to be on https
;
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
// for in memory authentication
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.passwordEncoder(passwordEncoder())
.withUser("user")
.password(passwordEncoder().encode("password"))
.roles("USER");
}
}
You can get logged in User details anytime in controller by below code
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String name = auth.getName(); //get logged in username
add a comment |
up vote
0
down vote
Thanks for following the approach i guided on
https://stackoverflow.com/a/53371025/10232467
Authentication and Authorization process is not defined in your given source code. We should use Spring Security to do this job. Please import spring security dependencies and make Security config class as below
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
// .loginPage("/login") in case if custom login page is required
.permitAll()
.and()
.logout()
.permitAll()
// add ant matchers and require secure in case if certain url has to be on https
;
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
// for in memory authentication
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.passwordEncoder(passwordEncoder())
.withUser("user")
.password(passwordEncoder().encode("password"))
.roles("USER");
}
}
You can get logged in User details anytime in controller by below code
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String name = auth.getName(); //get logged in username
add a comment |
up vote
0
down vote
up vote
0
down vote
Thanks for following the approach i guided on
https://stackoverflow.com/a/53371025/10232467
Authentication and Authorization process is not defined in your given source code. We should use Spring Security to do this job. Please import spring security dependencies and make Security config class as below
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
// .loginPage("/login") in case if custom login page is required
.permitAll()
.and()
.logout()
.permitAll()
// add ant matchers and require secure in case if certain url has to be on https
;
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
// for in memory authentication
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.passwordEncoder(passwordEncoder())
.withUser("user")
.password(passwordEncoder().encode("password"))
.roles("USER");
}
}
You can get logged in User details anytime in controller by below code
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String name = auth.getName(); //get logged in username
Thanks for following the approach i guided on
https://stackoverflow.com/a/53371025/10232467
Authentication and Authorization process is not defined in your given source code. We should use Spring Security to do this job. Please import spring security dependencies and make Security config class as below
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
// .loginPage("/login") in case if custom login page is required
.permitAll()
.and()
.logout()
.permitAll()
// add ant matchers and require secure in case if certain url has to be on https
;
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
// for in memory authentication
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.passwordEncoder(passwordEncoder())
.withUser("user")
.password(passwordEncoder().encode("password"))
.roles("USER");
}
}
You can get logged in User details anytime in controller by below code
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String name = auth.getName(); //get logged in username
edited Nov 27 at 6:24
answered Nov 27 at 6:17
EspringDev
384
384
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417014%2fwhy-do-i-have-a-login-issue-after-updating-rows-in-my-database%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Could you add your whole html form and your model's code?
– Alain Cruz
Nov 21 at 17:11
Show your
savemethod of theUserService.– M. Deinum
Nov 21 at 17:44
I added full code below
– Krzysztof Sawicki
Nov 21 at 22:27