many changes
This commit is contained in:
parent
128e330342
commit
b65237d9eb
@ -1,22 +1,23 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?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"
|
<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">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-parent</artifactId>
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
<version>2.1.8.RELEASE</version>
|
<version>2.1.8.RELEASE</version>
|
||||||
<relativePath/> <!-- lookup parent from repository -->
|
<relativePath /> <!-- lookup parent from repository -->
|
||||||
</parent>
|
</parent>
|
||||||
<groupId>org.ros</groupId>
|
<groupId>org.ros</groupId>
|
||||||
<artifactId>Chatto</artifactId>
|
<artifactId>Chatto</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<packaging>war</packaging>
|
<packaging>jar</packaging>
|
||||||
<name>chatto</name>
|
<name>chatto</name>
|
||||||
<description>Demo project for Spring Boot</description>
|
<description>Demo project for Spring Boot</description>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<java.version>12</java.version>
|
<java.version>11</java.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
@ -58,6 +59,27 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-security</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.security</groupId>
|
||||||
|
<artifactId>spring-security-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.ulisesbocchio</groupId>
|
||||||
|
<artifactId>jasypt-spring-boot-starter</artifactId>
|
||||||
|
<version>2.1.2</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -3,13 +3,33 @@ package org.ros.chatto;
|
|||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||||
|
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
||||||
|
|
||||||
@EnableAutoConfiguration
|
@EnableAutoConfiguration
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class ChattoApplication {
|
public class ChattoApplication extends SpringBootServletInitializer {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(ChattoApplication.class, args);
|
SpringApplication.run(ChattoApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
|
||||||
|
return application.sources(ChattoApplication.class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//public class Application extends SpringBootServletInitializer {
|
||||||
|
//
|
||||||
|
// public static void main(String[] args) {
|
||||||
|
// SpringApplication.run(applicationClass, args);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
|
||||||
|
// return application.sources(applicationClass);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// private static Class<Application> applicationClass = Application.class;
|
||||||
|
//}
|
@ -0,0 +1,113 @@
|
|||||||
|
package org.ros.chatto;
|
||||||
|
|
||||||
|
import org.ros.chatto.security.MyUserDetailsService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.security.authentication.AuthenticationProvider;
|
||||||
|
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
|
||||||
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||||
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
|
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
||||||
|
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableWebSecurity
|
||||||
|
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private AuthenticationSuccessHandler authenticationSuccessHandler;
|
||||||
|
@Autowired
|
||||||
|
private MyUserDetailsService myUserDetailsService;
|
||||||
|
@Autowired
|
||||||
|
private PasswordEncoder passwordEncoder;
|
||||||
|
// @SuppressWarnings("deprecation")
|
||||||
|
@Bean
|
||||||
|
public AuthenticationProvider authenticationProvider()
|
||||||
|
{
|
||||||
|
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
|
||||||
|
provider.setUserDetailsService(myUserDetailsService);
|
||||||
|
provider.setPasswordEncoder(passwordEncoder);
|
||||||
|
return provider;
|
||||||
|
}
|
||||||
|
@Bean
|
||||||
|
public static PasswordEncoder passwordEncoder() {
|
||||||
|
return new BCryptPasswordEncoder();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void configure(HttpSecurity httpSecurity) throws Exception {
|
||||||
|
httpSecurity.authorizeRequests()
|
||||||
|
.antMatchers("/","/login*","/registration","/perform_registration","/css/**", "/js/**", "/images/**").permitAll()
|
||||||
|
.antMatchers("/user/**").hasAnyRole("USER", "ADMIN","SUPER_USER")
|
||||||
|
.antMatchers("/admin/**","/api/**").hasAnyRole("ADMIN","SUPER_USER")
|
||||||
|
.anyRequest()
|
||||||
|
.authenticated()
|
||||||
|
.and()
|
||||||
|
.formLogin()
|
||||||
|
// .loginPage("/login").permitAll()
|
||||||
|
.loginProcessingUrl("/perform_login")
|
||||||
|
.successHandler(authenticationSuccessHandler)
|
||||||
|
.failureUrl("/?login_error")
|
||||||
|
.and()
|
||||||
|
.logout().invalidateHttpSession(true)
|
||||||
|
.clearAuthentication(true)
|
||||||
|
.logoutRequestMatcher(new AntPathRequestMatcher("/perform_logout"))
|
||||||
|
.logoutSuccessUrl("/").permitAll();;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// httpSecurity
|
||||||
|
// .csrf().disable()
|
||||||
|
// .authorizeRequests().antMatchers("login").permitAll()
|
||||||
|
// .anyRequest().authenticated()
|
||||||
|
// .and()
|
||||||
|
// .formLogin()
|
||||||
|
// .loginPage("/login").permitAll()
|
||||||
|
// .and()
|
||||||
|
// .logout().invalidateHttpSession(true)
|
||||||
|
// .clearAuthentication(true)
|
||||||
|
// .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
|
||||||
|
// .logoutSuccessUrl("/").permitAll();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Override
|
||||||
|
// protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
||||||
|
// auth.inMemoryAuthentication()
|
||||||
|
// .withUser("user")
|
||||||
|
// .password("{noop}user")
|
||||||
|
// .roles("USER")
|
||||||
|
// .and()
|
||||||
|
// .withUser("admin")
|
||||||
|
// .password("{noop}admin")
|
||||||
|
// .roles("ADMIN");
|
||||||
|
//// auth.userDetailsService(myUserDetailsService);
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
// @Bean
|
||||||
|
// @Override
|
||||||
|
// public UserDetailsService userDetailsService(String usern) {
|
||||||
|
//// UserDetails user =
|
||||||
|
//// User.withDefaultPasswordEncoder()
|
||||||
|
//// .username("user")
|
||||||
|
//// .password("password")
|
||||||
|
//// .roles("USER")
|
||||||
|
//// .build();
|
||||||
|
////
|
||||||
|
//// return new InMemoryUserDetailsManager(user);
|
||||||
|
// myUserDetailsService.loadUserByUsername(username)
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
// @Override
|
||||||
|
// protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
||||||
|
// auth.userDetailsService(myUserDetailsService);
|
||||||
|
// }
|
||||||
|
// auth.userDetailsService(myUserDetailsService);
|
||||||
|
|
||||||
|
// }
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package org.ros.chatto.controller;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequestMapping("/admin")
|
||||||
|
public class AdminController {
|
||||||
|
|
||||||
|
@RequestMapping
|
||||||
|
public String viewManageUsers() {
|
||||||
|
return "/admin/home";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package org.ros.chatto.controller;
|
||||||
|
|
||||||
|
import org.ros.chatto.security.AuthenticationSuccessHandlerImpl;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class BeanConfigurations {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public AuthenticationSuccessHandler authenticationSuccessHandler() {
|
||||||
|
return new AuthenticationSuccessHandlerImpl();
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,8 @@
|
|||||||
package org.ros.chatto.controller;
|
package org.ros.chatto.controller;
|
||||||
|
|
||||||
import org.ros.chatto.repository.UserRepositoryCustomInterface;
|
import org.ros.chatto.repository.UserRepositoryCustom;
|
||||||
|
import org.ros.chatto.repository.UserRoleRepository;
|
||||||
|
import org.ros.chatto.repository.RoleRepository;
|
||||||
import org.ros.chatto.repository.UserRepository;
|
import org.ros.chatto.repository.UserRepository;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
@ -10,6 +12,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.ros.chatto.model.ChatUser;
|
import org.ros.chatto.model.ChatUser;
|
||||||
|
import org.ros.chatto.model.UserRole;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api")
|
@RequestMapping("/api")
|
||||||
@ -18,7 +21,11 @@ public class DemoRestController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
UserRepository userRepository;
|
UserRepository userRepository;
|
||||||
@Autowired
|
@Autowired
|
||||||
UserRepositoryCustomInterface userRepositoryCustomInterface;
|
UserRepositoryCustom userRepositoryCustom;
|
||||||
|
@Autowired
|
||||||
|
RoleRepository roleRepository;
|
||||||
|
@Autowired
|
||||||
|
UserRoleRepository userRoleRepository;
|
||||||
|
|
||||||
@GetMapping("/users")
|
@GetMapping("/users")
|
||||||
public List<ChatUser> getAllUsers() {
|
public List<ChatUser> getAllUsers() {
|
||||||
@ -27,7 +34,7 @@ public class DemoRestController {
|
|||||||
|
|
||||||
@GetMapping("/usernames")
|
@GetMapping("/usernames")
|
||||||
public List<String> getUserNames() {
|
public List<String> getUserNames() {
|
||||||
return userRepositoryCustomInterface.getAllUserNames("hmm");
|
return userRepositoryCustom.getAllUserNames("hmm");
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/user")
|
@GetMapping("/user")
|
||||||
@ -35,4 +42,10 @@ public class DemoRestController {
|
|||||||
return userRepository.findByUserName("hmm");
|
return userRepository.findByUserName("hmm");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/roles")
|
||||||
|
public List<UserRole> getAllRoles()
|
||||||
|
{
|
||||||
|
return userRoleRepository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
package org.ros.chatto.controller;
|
package org.ros.chatto.controller;
|
||||||
import org.ros.chatto.repository.UserRepositoryCustomInterface;
|
import org.ros.chatto.repository.UserRepositoryCustom;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.servlet.ModelAndView;
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
|
|
||||||
@ -23,15 +22,14 @@ public class TestController {
|
|||||||
}*/
|
}*/
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping("/home")
|
|
||||||
public class Home {
|
public class Home {
|
||||||
@Autowired
|
@Autowired
|
||||||
UserRepositoryCustomInterface userRepositoryCustomInterface;
|
UserRepositoryCustom userRepositoryCustom;
|
||||||
@GetMapping
|
@RequestMapping("/")
|
||||||
public ModelAndView showPage() {
|
public ModelAndView showPage() {
|
||||||
ModelAndView mv = new ModelAndView("home");
|
ModelAndView mv = new ModelAndView("home");
|
||||||
mv.addObject("message", "Welcome!");
|
mv.addObject("message", "Welcome!");
|
||||||
mv.addObject("userNames", userRepositoryCustomInterface.getAllUserNames("hmm"));
|
mv.addObject("userNames", userRepositoryCustom.getAllUserNames("hmm"));
|
||||||
return mv;
|
return mv;
|
||||||
}
|
}
|
||||||
// public String showHome(Model model)
|
// public String showHome(Model model)
|
||||||
|
@ -1,20 +1,28 @@
|
|||||||
package org.ros.chatto.controller;
|
package org.ros.chatto.controller;
|
||||||
|
|
||||||
import org.ros.chatto.model.ChatUser;
|
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.servlet.ModelAndView;
|
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping("/login")
|
|
||||||
public class Login {
|
public class Login {
|
||||||
@PostMapping
|
@GetMapping("/login")
|
||||||
public ModelAndView login(@ModelAttribute ChatUser chatUser)
|
public String loginForm() {
|
||||||
|
// model.addAttribute("user", new ChatUser());
|
||||||
|
return "login";
|
||||||
|
}
|
||||||
|
// @PostMapping("/login")
|
||||||
|
// public String loginSubmit(@ModelAttribute ChatUser chatUser)
|
||||||
|
// {
|
||||||
|
// if(chatUser.getUserName().equalsIgnoreCase("") || chatUser.getPassword().equalsIgnoreCase("")) {
|
||||||
|
// return "error";
|
||||||
|
// }
|
||||||
|
//// System.out.println(chatUser.getPassword());
|
||||||
|
// return "user";
|
||||||
|
// }
|
||||||
|
|
||||||
|
@GetMapping("logout-success")
|
||||||
|
public String doLogout()
|
||||||
{
|
{
|
||||||
ModelAndView mv = new ModelAndView("login");
|
return "logout";
|
||||||
mv.addObject(chatUser);
|
|
||||||
return mv;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,34 @@
|
|||||||
|
package org.ros.chatto.controller;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import org.ros.chatto.model.UserDTO;
|
||||||
|
import org.ros.chatto.service.UserService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public class RegisterController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserService userService;
|
||||||
|
@GetMapping("/registration")
|
||||||
|
public ModelAndView registrationForm()
|
||||||
|
{
|
||||||
|
ModelAndView modelAndView = new ModelAndView("registration");
|
||||||
|
modelAndView.addObject("userDTO",new UserDTO());
|
||||||
|
return modelAndView;
|
||||||
|
}
|
||||||
|
@PostMapping("/perform_registration")
|
||||||
|
public ModelAndView performRegistration(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, @ModelAttribute("userDTO") UserDTO userDTO)
|
||||||
|
{
|
||||||
|
ModelAndView modelAndView = new ModelAndView("user/home");
|
||||||
|
userService.registerUser(userDTO);
|
||||||
|
return modelAndView;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package org.ros.chatto.controller;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequestMapping("/user")
|
||||||
|
public class UserController {
|
||||||
|
|
||||||
|
@RequestMapping
|
||||||
|
public String viewUserProfile() {
|
||||||
|
return "user/home";
|
||||||
|
}
|
||||||
|
}
|
36
chatto/src/main/java/org/ros/chatto/model/ChatMessage.java
Normal file
36
chatto/src/main/java/org/ros/chatto/model/ChatMessage.java
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package org.ros.chatto.model;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import javax.persistence.Column;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.GenerationType;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.JoinColumn;
|
||||||
|
import javax.persistence.OneToOne;
|
||||||
|
import javax.persistence.Table;
|
||||||
|
import javax.persistence.Temporal;
|
||||||
|
import javax.persistence.TemporalType;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import net.bytebuddy.asm.Advice.This;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Entity
|
||||||
|
@Table(name = "chat_messages")
|
||||||
|
public class ChatMessage {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
@Column(name = "m_id")
|
||||||
|
private Long messageID;
|
||||||
|
@OneToOne
|
||||||
|
@JoinColumn(name = "from_user")
|
||||||
|
private ChatUser fromUser;
|
||||||
|
@OneToOne
|
||||||
|
@JoinColumn(name = "to_user")
|
||||||
|
private ChatUser toUser;
|
||||||
|
private String message;
|
||||||
|
@Temporal(TemporalType.TIMESTAMP)
|
||||||
|
private Date messageTime;
|
||||||
|
}
|
@ -1,76 +1,101 @@
|
|||||||
package org.ros.chatto.model;
|
package org.ros.chatto.model;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import javax.persistence.CascadeType;
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.EntityListeners;
|
import javax.persistence.EntityListeners;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.GenerationType;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.JoinTable;
|
||||||
|
import javax.persistence.ManyToMany;
|
||||||
|
import javax.persistence.OneToMany;
|
||||||
|
import javax.persistence.SequenceGenerator;
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
import javax.persistence.Temporal;
|
import javax.persistence.Temporal;
|
||||||
import javax.persistence.TemporalType;
|
import javax.persistence.TemporalType;
|
||||||
|
import javax.persistence.JoinColumn;
|
||||||
|
|
||||||
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonBackReference;
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "users")
|
@Table(name = "users")
|
||||||
@EntityListeners(AuditingEntityListener.class)
|
@EntityListeners(AuditingEntityListener.class)
|
||||||
@JsonIgnoreProperties(value = {"password", "salt"},
|
@JsonIgnoreProperties(value = { "password"}, allowGetters = false)
|
||||||
allowGetters = false)
|
|
||||||
|
|
||||||
public class ChatUser {
|
public class ChatUser {
|
||||||
@Id
|
@Id
|
||||||
@Column(name="user_id")
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
// @SequenceGenerator(name="user_generator", sequenceName = "user_seq", allocationSize=50)
|
||||||
|
@Column(name = "user_id")
|
||||||
private int userID;
|
private int userID;
|
||||||
@Column(name = "name")
|
@Column(name = "name")
|
||||||
private String userName;
|
private String userName;
|
||||||
String password, salt;
|
String password;
|
||||||
@Temporal(TemporalType.TIMESTAMP)
|
@Temporal(TemporalType.TIMESTAMP)
|
||||||
private Date joinDate;
|
private Date joinDate;
|
||||||
|
// @ManyToMany(cascade = CascadeType.ALL)
|
||||||
|
// @JoinTable(name = "users_roles", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
|
||||||
|
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
|
||||||
|
@JsonBackReference
|
||||||
|
private Set<UserRole> userRoles = new HashSet<UserRole>();
|
||||||
|
|
||||||
public int getUserID() {
|
public int getUserID() {
|
||||||
return userID;
|
return userID;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUserID(int userID) {
|
public void setUserID(int userID) {
|
||||||
this.userID = userID;
|
this.userID = userID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public String getUserName() {
|
public String getUserName() {
|
||||||
return userName;
|
return userName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUserName(String userName) {
|
public void setUserName(String userName) {
|
||||||
this.userName = userName;
|
this.userName = userName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPassword() {
|
public String getPassword() {
|
||||||
return password;
|
return password;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPassword(String password) {
|
public void setPassword(String password) {
|
||||||
this.password = password;
|
this.password = password;
|
||||||
}
|
}
|
||||||
public String getSalt() {
|
|
||||||
return salt;
|
|
||||||
}
|
|
||||||
public void setSalt(String salt) {
|
|
||||||
this.salt = salt;
|
|
||||||
}
|
|
||||||
public Date getJoinDate() {
|
public Date getJoinDate() {
|
||||||
return joinDate;
|
return joinDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setJoinDate(Date joinDate) {
|
public void setJoinDate(Date joinDate) {
|
||||||
this.joinDate = joinDate;
|
this.joinDate = joinDate;
|
||||||
}
|
}
|
||||||
public ChatUser(int userID, String userName, String password, String salt, Date joinDate) {
|
|
||||||
|
public ChatUser(int userID, String userName, String password, Date joinDate) {
|
||||||
super();
|
super();
|
||||||
this.userID = userID;
|
this.userID = userID;
|
||||||
this.userName = userName;
|
this.userName = userName;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
this.salt = salt;
|
|
||||||
this.joinDate = joinDate;
|
this.joinDate = joinDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ChatUser() {}
|
public ChatUser() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<UserRole> getUserRoles() {
|
||||||
|
return userRoles;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserRoles(Set<UserRole> userRoles) {
|
||||||
|
this.userRoles = userRoles;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
46
chatto/src/main/java/org/ros/chatto/model/Role.java
Normal file
46
chatto/src/main/java/org/ros/chatto/model/Role.java
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
package org.ros.chatto.model;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import javax.persistence.Column;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.OneToMany;
|
||||||
|
import javax.persistence.Table;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonBackReference;
|
||||||
|
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "roles")
|
||||||
|
public class Role {
|
||||||
|
@Id
|
||||||
|
@Column(name = "role_id")
|
||||||
|
private int roleID;
|
||||||
|
@Column(name = "role_name")
|
||||||
|
private String name;
|
||||||
|
private String description;
|
||||||
|
@OneToMany(mappedBy = "role")
|
||||||
|
@JsonBackReference
|
||||||
|
private Set<UserRole> userRoles = new HashSet<>();
|
||||||
|
public int getRoleId() {
|
||||||
|
return roleID;
|
||||||
|
}
|
||||||
|
public void setRoleId(int id) {
|
||||||
|
this.roleID = id;
|
||||||
|
}
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
public void setName(String role) {
|
||||||
|
this.name = role;
|
||||||
|
}
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
public void setDescription(String description) {
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
23
chatto/src/main/java/org/ros/chatto/model/UserDTO.java
Normal file
23
chatto/src/main/java/org/ros/chatto/model/UserDTO.java
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package org.ros.chatto.model;
|
||||||
|
|
||||||
|
import javax.persistence.Transient;
|
||||||
|
|
||||||
|
public class UserDTO {
|
||||||
|
private String userName;
|
||||||
|
@Transient
|
||||||
|
private String password;
|
||||||
|
public String getUserName() {
|
||||||
|
return userName;
|
||||||
|
}
|
||||||
|
public void setUserName(String userName) {
|
||||||
|
this.userName = userName;
|
||||||
|
}
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
51
chatto/src/main/java/org/ros/chatto/model/UserRole.java
Normal file
51
chatto/src/main/java/org/ros/chatto/model/UserRole.java
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
package org.ros.chatto.model;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.JoinColumn;
|
||||||
|
import javax.persistence.ManyToOne;
|
||||||
|
import javax.persistence.Table;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonManagedReference;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "users_roles")
|
||||||
|
public class UserRole {
|
||||||
|
@Id
|
||||||
|
private int id;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name = "user_id")
|
||||||
|
@JsonManagedReference
|
||||||
|
private ChatUser user;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name = "role_id")
|
||||||
|
@JsonManagedReference
|
||||||
|
private Role role;
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ChatUser getUser() {
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUser(ChatUser user) {
|
||||||
|
this.user = user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Role getRole() {
|
||||||
|
return role;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRole(Role role) {
|
||||||
|
this.role = role;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package org.ros.chatto.repository;
|
||||||
|
|
||||||
|
import org.ros.chatto.model.ChatMessage;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
public interface ChatMessageRepository extends JpaRepository<ChatMessage, Long> {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package org.ros.chatto.repository;
|
||||||
|
|
||||||
|
import org.ros.chatto.model.Role;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface RoleRepository extends JpaRepository<Role, Long>{
|
||||||
|
// @Query("select r from RoleRepository where name = ?1")
|
||||||
|
public Role findByName(String roleName);
|
||||||
|
}
|
@ -2,43 +2,10 @@ package org.ros.chatto.repository;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
public interface UserRepositoryCustom {
|
||||||
import javax.persistence.PersistenceContext;
|
|
||||||
import javax.persistence.criteria.CriteriaBuilder;
|
|
||||||
import javax.persistence.criteria.CriteriaQuery;
|
|
||||||
import javax.persistence.criteria.Root;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.ros.chatto.model.ChatUser;
|
|
||||||
|
|
||||||
import org.ros.chatto.repository.UserRepositoryCustomInterface;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
class UserRepositoryCustom implements UserRepositoryCustomInterface{
|
|
||||||
@PersistenceContext
|
|
||||||
private EntityManager entityManager;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<String> getAllUserNames(String userName) {
|
|
||||||
List<String> userNamesList = null;
|
|
||||||
// Session session = null;
|
|
||||||
try {
|
|
||||||
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
|
||||||
CriteriaQuery<String> criteriaQuery = criteriaBuilder.createQuery(String.class);
|
|
||||||
Root<ChatUser> root = criteriaQuery.from(ChatUser.class);
|
|
||||||
criteriaQuery.select(root.get("userName"));
|
|
||||||
criteriaQuery.where(criteriaBuilder.notEqual(root.get("userName"), userName));
|
|
||||||
|
|
||||||
userNamesList = entityManager.createQuery(criteriaQuery).getResultList();
|
|
||||||
for(String un: userNamesList)
|
|
||||||
{
|
|
||||||
System.out.println(un);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
// TODO: handle exception
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return userNamesList;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// @Query("select s from Article s where s.author like ?1 and s.title = ?2")
|
||||||
|
// List<Article> findByAuthorAndTitle(String author, String title);
|
||||||
|
// @Query("select u from ChatUser u")
|
||||||
|
public List<String> getAllUserNames(String s);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,44 @@
|
|||||||
|
package org.ros.chatto.repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.PersistenceContext;
|
||||||
|
import javax.persistence.criteria.CriteriaBuilder;
|
||||||
|
import javax.persistence.criteria.CriteriaQuery;
|
||||||
|
import javax.persistence.criteria.Root;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.ros.chatto.model.ChatUser;
|
||||||
|
|
||||||
|
import org.ros.chatto.repository.UserRepositoryCustom;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
class UserRepositoryCustomImpl implements UserRepositoryCustom{
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager entityManager;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> getAllUserNames(String userName) {
|
||||||
|
List<String> userNamesList = null;
|
||||||
|
// Session session = null;
|
||||||
|
try {
|
||||||
|
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||||
|
CriteriaQuery<String> criteriaQuery = criteriaBuilder.createQuery(String.class);
|
||||||
|
Root<ChatUser> root = criteriaQuery.from(ChatUser.class);
|
||||||
|
criteriaQuery.select(root.get("userName"));
|
||||||
|
criteriaQuery.where(criteriaBuilder.notEqual(root.get("userName"), userName));
|
||||||
|
|
||||||
|
userNamesList = entityManager.createQuery(criteriaQuery).getResultList();
|
||||||
|
for(String un: userNamesList)
|
||||||
|
{
|
||||||
|
System.out.println(un);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
// TODO: handle exception
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return userNamesList;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,11 +0,0 @@
|
|||||||
package org.ros.chatto.repository;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public interface UserRepositoryCustomInterface {
|
|
||||||
|
|
||||||
// @Query("select s from Article s where s.author like ?1 and s.title = ?2")
|
|
||||||
// List<Article> findByAuthorAndTitle(String author, String title);
|
|
||||||
// @Query("select u from ChatUser u")
|
|
||||||
public List<String> getAllUserNames(String s);
|
|
||||||
}
|
|
@ -0,0 +1,14 @@
|
|||||||
|
package org.ros.chatto.repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.ros.chatto.model.UserRole;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface UserRoleRepository extends JpaRepository<UserRole, Long>{
|
||||||
|
@Query("select ur from UserRole ur where ur.user.userID = ?1")
|
||||||
|
public List<UserRole> findByUser(int userID);
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
package org.ros.chatto.security;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.core.GrantedAuthority;
|
||||||
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||||
|
import org.springframework.security.web.DefaultRedirectStrategy;
|
||||||
|
import org.springframework.security.web.RedirectStrategy;
|
||||||
|
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class AuthenticationSuccessHandlerImpl implements AuthenticationSuccessHandler {
|
||||||
|
|
||||||
|
private static final SimpleGrantedAuthority SUPER_USER_AUTHORITY = new SimpleGrantedAuthority("ROLE_SUPER_USER");
|
||||||
|
private static final SimpleGrantedAuthority ADMIN_AUTHORITY = new SimpleGrantedAuthority("ROLE_ADMIN");
|
||||||
|
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
|
||||||
|
Authentication authentication) throws IOException, ServletException {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
|
||||||
|
if (authorities.contains(ADMIN_AUTHORITY) || authorities.contains(SUPER_USER_AUTHORITY)) {
|
||||||
|
redirectStrategy.sendRedirect(httpServletRequest, httpServletResponse, "/admin");
|
||||||
|
} else {
|
||||||
|
redirectStrategy.sendRedirect(httpServletRequest, httpServletResponse, "/user");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,75 @@
|
|||||||
|
package org.ros.chatto.security;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
|
||||||
|
import org.ros.chatto.model.ChatUser;
|
||||||
|
import org.ros.chatto.model.UserRole;
|
||||||
|
import org.ros.chatto.repository.RoleRepository;
|
||||||
|
import org.ros.chatto.repository.UserRepository;
|
||||||
|
import org.ros.chatto.repository.UserRoleRepository;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.security.core.userdetails.User;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||||
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class MyUserDetailsService implements UserDetailsService {
|
||||||
|
|
||||||
|
// @Autowired
|
||||||
|
// private WebApplicationContext applicationContext;
|
||||||
|
@Autowired
|
||||||
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
// @Autowired
|
||||||
|
// private RoleRepository roleRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserRoleRepository userRoleRepository;
|
||||||
|
// @PostConstruct
|
||||||
|
// public void completeSetup() {
|
||||||
|
// userRepository = applicationContext.getBean(UserRepository.class);
|
||||||
|
// }
|
||||||
|
|
||||||
|
public MyUserDetailsService() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public UserDetails loadUserByUsername(String username) {
|
||||||
|
ChatUser user = userRepository.findByUserName(username);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if (user == null) {
|
||||||
|
throw new UsernameNotFoundException(username);
|
||||||
|
}
|
||||||
|
System.out.println("Found useeeeeeeeeeeeeeeeeeeeeeeeeeeeeeer " + user.getUserName() + user.getPassword());
|
||||||
|
List<UserRole> userRoles = userRoleRepository.findByUser(user.getUserID());
|
||||||
|
System.out.println("User role iddddddddddddddddd = " + userRoles.get(0).getRole().getName());
|
||||||
|
// System.out.println(userRoles.);
|
||||||
|
// return new MyUserPrincipal(user);
|
||||||
|
return toUserDetails(new UserObject(user.getUserName(), user.getPassword(), userRoles.get(0).getRole().getName()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private UserDetails toUserDetails(UserObject userObject) {
|
||||||
|
return User.withUsername(userObject.name)
|
||||||
|
.password(userObject.password)
|
||||||
|
.roles(userObject.role).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class UserObject {
|
||||||
|
private String name;
|
||||||
|
private String password;
|
||||||
|
private String role;
|
||||||
|
|
||||||
|
public UserObject(String name, String password, String role) {
|
||||||
|
this.name = name;
|
||||||
|
this.password = password;
|
||||||
|
this.role = role;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,71 @@
|
|||||||
|
package org.ros.chatto.security;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
import org.ros.chatto.model.ChatUser;
|
||||||
|
import org.springframework.security.core.GrantedAuthority;
|
||||||
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
|
||||||
|
public class MyUserPrincipal implements UserDetails {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = -2761445275537412028L;
|
||||||
|
private ChatUser user;
|
||||||
|
|
||||||
|
public MyUserPrincipal(ChatUser user) {
|
||||||
|
super();
|
||||||
|
this.user = user;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<? extends GrantedAuthority> getAuthorities() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return Collections.singleton(new SimpleGrantedAuthority("USER"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPassword() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return user.getPassword();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getUsername() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return user.getUserName();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isAccountNonExpired() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isAccountNonLocked() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCredentialsNonExpired() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEnabled() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ChatUser getChatUser()
|
||||||
|
{
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package org.ros.chatto.service;
|
||||||
|
|
||||||
|
import org.ros.chatto.model.Role;
|
||||||
|
|
||||||
|
public interface RoleService {
|
||||||
|
Role getRole(String roleName);
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package org.ros.chatto.service;
|
||||||
|
|
||||||
|
import org.ros.chatto.model.Role;
|
||||||
|
import org.ros.chatto.repository.RoleRepository;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class RoleServiceImpl implements RoleService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RoleRepository roleRepository;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Role getRole(String roleName) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
// Role role = new Role();
|
||||||
|
return roleRepository.findByName(roleName);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
11
chatto/src/main/java/org/ros/chatto/service/UserService.java
Normal file
11
chatto/src/main/java/org/ros/chatto/service/UserService.java
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package org.ros.chatto.service;
|
||||||
|
|
||||||
|
import org.ros.chatto.model.ChatUser;
|
||||||
|
import org.ros.chatto.model.UserDTO;
|
||||||
|
|
||||||
|
//@Service
|
||||||
|
public interface UserService {
|
||||||
|
public void saveChatUser(ChatUser user);
|
||||||
|
|
||||||
|
public void registerUser(UserDTO userDTO);
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
package org.ros.chatto.service;
|
||||||
|
|
||||||
|
import org.ros.chatto.model.ChatUser;
|
||||||
|
import org.ros.chatto.model.Role;
|
||||||
|
import org.ros.chatto.model.UserDTO;
|
||||||
|
import org.ros.chatto.model.UserRole;
|
||||||
|
import org.ros.chatto.repository.UserRepository;
|
||||||
|
import org.ros.chatto.repository.UserRoleRepository;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class UserServiceImpl implements UserService{
|
||||||
|
@Autowired
|
||||||
|
UserRepository userRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
UserRoleRepository userRoleRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
RoleService roleService;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void saveChatUser(ChatUser user) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
ChatUser changedUser = userRepository.save(user);
|
||||||
|
UserRole userRole = new UserRole();
|
||||||
|
userRole.setRole(roleService.getRole("USER"));
|
||||||
|
userRole.setUser(changedUser);
|
||||||
|
userRoleRepository.save(userRole);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerUser(UserDTO userDTO) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
ChatUser user = new ChatUser();
|
||||||
|
user.setUserName(userDTO.getUserName());
|
||||||
|
user.setPassword(passwordEncoder.encode(userDTO.getPassword()));
|
||||||
|
ChatUser changedUser = userRepository.save(user);
|
||||||
|
UserRole userRole = new UserRole();
|
||||||
|
Role role = roleService.getRole("USER");
|
||||||
|
userRole.setRole(role);
|
||||||
|
userRole.setUser(changedUser);
|
||||||
|
System.out.println(role.getRoleId());
|
||||||
|
System.out.println(changedUser.getUserID());
|
||||||
|
userRoleRepository.save(userRole);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -9,6 +9,7 @@ spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
|||||||
# The SQL dialect makes Hibernate generate better SQL for the chosen database
|
# The SQL dialect makes Hibernate generate better SQL for the chosen database
|
||||||
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
|
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
|
||||||
|
|
||||||
|
|
||||||
# Hibernate ddl auto (create, create-drop, validate, update)
|
# Hibernate ddl auto (create, create-drop, validate, update)
|
||||||
spring.jpa.hibernate.ddl-auto = validate
|
spring.jpa.hibernate.ddl-auto = validate
|
||||||
|
|
||||||
@ -16,3 +17,4 @@ logging.level.org.springframework.web=DEBUG
|
|||||||
logging.level.web=DEBUG
|
logging.level.web=DEBUG
|
||||||
logging.level.org.hibernate.SQL=DEBUG
|
logging.level.org.hibernate.SQL=DEBUG
|
||||||
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
|
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
|
||||||
|
spring.http.log-request-details=true
|
13
chatto/src/main/resources/templates/admin/home.html
Normal file
13
chatto/src/main/resources/templates/admin/home.html
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Insert title here</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
admin page
|
||||||
|
<form action="#" th:action="@{/perform_logout}" method="POST">
|
||||||
|
<input type="submit" value="logout">
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -7,8 +7,13 @@
|
|||||||
<div>
|
<div>
|
||||||
Web Application. Passed parameter : <span th:text="${message}"></span>
|
Web Application. Passed parameter : <span th:text="${message}"></span>
|
||||||
</div>
|
</div>
|
||||||
|
<!-- <p th:if="${chatUser}" th:text="'username: ' + ${chatUser.userName}">You need to login</p>
|
||||||
<th:block th:each="userName: ${userNames}">
|
<th:block th:each="userName: ${userNames}">
|
||||||
<div th:text="${userName}"></div>
|
<div th:text="${userName}"></div>
|
||||||
</th:block>
|
</th:block> -->
|
||||||
|
<p>Welcome to home page. Please login to access any features.</p>
|
||||||
|
<a href="login">login</a>
|
||||||
|
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
30
chatto/src/main/resources/templates/login.html
Normal file
30
chatto/src/main/resources/templates/login.html
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<!DOCTYPE HTML>
|
||||||
|
<html xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<head>
|
||||||
|
<title>Title</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div>Login Page</div>
|
||||||
|
|
||||||
|
<!-- <form action="#" th:action="@{/greeting}" th:object="${greeting}" method="post">
|
||||||
|
<p>Id: <input type="text" th:field="*{id}" /></p>
|
||||||
|
<p>Message: <input type="text" th:field="*{content}" /></p>
|
||||||
|
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
|
||||||
|
</form> -->
|
||||||
|
<!-- <form action="#" th:action="@{/perform_login}" th:object=${user} method="POST">
|
||||||
|
<label>Enter user name: </label>
|
||||||
|
<input th:field="*{userName}" type="text" name="username" id="username">
|
||||||
|
<br> <br> <label>Enter password: </label>
|
||||||
|
<input th:field="*{password}" type="password" name="password" id="username"> <br> <br>
|
||||||
|
<input type="submit" value="Submit">
|
||||||
|
</form>
|
||||||
|
-->
|
||||||
|
<form action="#" th:action="@{/perform_login}" method="POST">
|
||||||
|
<label>Enter user name: </label>
|
||||||
|
<input type="text" name="username" id="username">
|
||||||
|
<br> <br> <label>Enter password: </label>
|
||||||
|
<input type="password" name="password" id="username"> <br> <br>
|
||||||
|
<input type="submit" value="Submit">
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
10
chatto/src/main/resources/templates/logout.html
Normal file
10
chatto/src/main/resources/templates/logout.html
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Insert title here</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
17
chatto/src/main/resources/templates/registration.html
Normal file
17
chatto/src/main/resources/templates/registration.html
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Insert title here</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<form action="#" th:action="@{/perform_registration}"
|
||||||
|
th:object=${userDTO} method="POST">
|
||||||
|
<label>Enter user name: </label> <input th:field="*{userName}"
|
||||||
|
type="text" name="username" id="username"> <br> <br>
|
||||||
|
<label>Enter password: </label> <input th:field="*{password}"
|
||||||
|
type="password" name="password" id="password"> <br> <br>
|
||||||
|
<input type="submit" value="Submit">
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
13
chatto/src/main/resources/templates/user/home.html
Normal file
13
chatto/src/main/resources/templates/user/home.html
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Insert title here</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
user page
|
||||||
|
<form action="#" th:action="@{/perform_logout}" method="POST">
|
||||||
|
<input type="submit" value="logout">
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -2,15 +2,69 @@ package org.ros.chatto;
|
|||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
import org.ros.chatto.model.ChatMessage;
|
||||||
|
import org.ros.chatto.model.ChatUser;
|
||||||
|
import org.ros.chatto.repository.ChatMessageRepository;
|
||||||
|
import org.ros.chatto.repository.UserRepository;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
public class ChattoApplicationTests {
|
public class ChattoApplicationTests {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
ChatMessageRepository chatMessageRepository;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
ChatMessageRepository mockChatMessageRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
UserRepository userRepository;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void contextLoads() {
|
public void contextLoads() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMessageRepo() {
|
||||||
|
chatMessageRepository.findAll().toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Test
|
||||||
|
// public void testSave() {
|
||||||
|
// ChatUser fromUser = new ChatUser();
|
||||||
|
// fromUser = userRepository.findByUserName("hmm");
|
||||||
|
// ChatUser toUser = new ChatUser();
|
||||||
|
// toUser = userRepository.findByUserName("user2");
|
||||||
|
// ChatMessage chatMessage = new ChatMessage();
|
||||||
|
// chatMessage.setMessage("Hello!");
|
||||||
|
// chatMessage.setFromUser(fromUser);
|
||||||
|
// chatMessage.setToUser(toUser);
|
||||||
|
//
|
||||||
|
// chatMessageRepository.save(chatMessage);
|
||||||
|
// }
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSave() {
|
||||||
|
ChatUser fromUser = new ChatUser();
|
||||||
|
fromUser = userRepository.findByUserName("hmm");
|
||||||
|
ChatUser toUser = new ChatUser();
|
||||||
|
toUser = userRepository.findByUserName("user2");
|
||||||
|
ChatMessage chatMessage = new ChatMessage();
|
||||||
|
chatMessage.setMessage("Hello!");
|
||||||
|
chatMessage.setFromUser(fromUser);
|
||||||
|
chatMessage.setToUser(toUser);
|
||||||
|
|
||||||
|
// chatMessageRepository.save(chatMessage);
|
||||||
|
when(mockChatMessageRepository.save(any(ChatMessage.class))).thenReturn(chatMessage);
|
||||||
|
verify(mockChatMessageRepository, times(1)).save(Mockito.any(ChatMessage.class));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user