This commit is contained in:
nkchauhan003
2022-12-15 16:14:09 +05:30
parent 670f2e251b
commit 267972e472
7 changed files with 70 additions and 8 deletions

View File

@@ -24,15 +24,15 @@ public class SpringSecurity {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/registration/**").permitAll()
.antMatchers("/login").permitAll()
.antMatchers("/user").hasAnyRole("USER", "ADMIN")
.antMatchers("/admin").hasAnyRole("ADMIN")
.antMatchers("/login/**").permitAll()
.antMatchers("/user/**").hasAnyRole("USER", "ADMIN")
.antMatchers("/admin/**").hasAnyRole("ADMIN")
.and()
.formLogin(
form -> form
.loginPage("/login")
.loginProcessingUrl("/login")
.defaultSuccessUrl("/user")
.defaultSuccessUrl("/user/")
.permitAll()
).logout(
logout -> logout

View File

@@ -0,0 +1,14 @@
package com.cb.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/user/")
public class UserController {
@GetMapping("/")
public String registrationForm() {
return "user";
}
}

View File

@@ -23,4 +23,8 @@ public class Role {
private String name;
@ManyToMany(mappedBy = "roles")
private List<User> users = new ArrayList<>();
public Role(String name) {
this.name = name;
}
}

View File

@@ -6,6 +6,7 @@ import com.cb.model.Role;
import com.cb.model.User;
import com.cb.repository.RoleRepository;
import com.cb.repository.UserRepository;
import com.cb.util.TbConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
@@ -28,8 +29,13 @@ public class UserServiceImpl implements UserService {
@Override
public void saveUser(UserDto userDto) {
Role role = roleRepository.findByName("ROLE_USER");
User user = new User(userDto.getName(),userDto.getEmail(),passwordEncoder.encode(userDto.getPassword()),Arrays.asList(role));
Role role = roleRepository.findByName(TbConstants.Roles.USER);
if (role == null)
role = roleRepository.save(new Role(TbConstants.Roles.USER));
User user = new User(userDto.getName(), userDto.getEmail(), passwordEncoder.encode(userDto.getPassword()),
Arrays.asList(role));
userRepository.save(user);
}

View File

@@ -0,0 +1,8 @@
package com.cb.util;
public class TbConstants {
public static interface Roles {
String USER = "ROLE_USER";
String ADMIN = "ROLE_ADMIN";
}
}

View File

@@ -6,7 +6,7 @@
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<title>Hello, world!</title>
<title>Login</title>
</head>
<body>
<div class="container">

View File

@@ -0,0 +1,30 @@
<!doctype html>
<html lang="en" xmlns:sec="http://www.thymeleaf.org/extras/spring-security" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<title>User</title>
</head>
<body>
<div class="container">
<div class="row mt-5">
<div class="col-6 mx-auto">
<h1>User Details</h1>
<div class="mt-2">Username: <span sec:authentication="principal.username"/></div>
<div class="mt-2"> Roles: <span sec:authentication="principal.authorities"/></div>
<div class="mt-5"><a th:href="@{/logout}" class="link-primary">Logout</a></div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
crossorigin="anonymous"></script>
</body>
</html>