Rohan Sircar
5 years ago
36 changed files with 977 additions and 93 deletions
-
30chatto/pom.xml
-
22chatto/src/main/java/org/ros/chatto/ChattoApplication.java
-
113chatto/src/main/java/org/ros/chatto/WebSecurityConfiguration.java
-
14chatto/src/main/java/org/ros/chatto/controller/AdminController.java
-
15chatto/src/main/java/org/ros/chatto/controller/BeanConfigurations.java
-
19chatto/src/main/java/org/ros/chatto/controller/DemoRestController.java
-
10chatto/src/main/java/org/ros/chatto/controller/Home.java
-
30chatto/src/main/java/org/ros/chatto/controller/Login.java
-
34chatto/src/main/java/org/ros/chatto/controller/RegisterController.java
-
14chatto/src/main/java/org/ros/chatto/controller/UserController.java
-
36chatto/src/main/java/org/ros/chatto/model/ChatMessage.java
-
61chatto/src/main/java/org/ros/chatto/model/ChatUser.java
-
46chatto/src/main/java/org/ros/chatto/model/Role.java
-
23chatto/src/main/java/org/ros/chatto/model/UserDTO.java
-
51chatto/src/main/java/org/ros/chatto/model/UserRole.java
-
8chatto/src/main/java/org/ros/chatto/repository/ChatMessageRepository.java
-
12chatto/src/main/java/org/ros/chatto/repository/RoleRepository.java
-
43chatto/src/main/java/org/ros/chatto/repository/UserRepositoryCustom.java
-
44chatto/src/main/java/org/ros/chatto/repository/UserRepositoryCustomImpl.java
-
11chatto/src/main/java/org/ros/chatto/repository/UserRepositoryCustomInterface.java
-
14chatto/src/main/java/org/ros/chatto/repository/UserRoleRepository.java
-
36chatto/src/main/java/org/ros/chatto/security/AuthenticationSuccessHandlerImpl.java
-
75chatto/src/main/java/org/ros/chatto/security/MyUserDetailsService.java
-
71chatto/src/main/java/org/ros/chatto/security/MyUserPrincipal.java
-
7chatto/src/main/java/org/ros/chatto/service/RoleService.java
-
21chatto/src/main/java/org/ros/chatto/service/RoleServiceImpl.java
-
11chatto/src/main/java/org/ros/chatto/service/UserService.java
-
53chatto/src/main/java/org/ros/chatto/service/UserServiceImpl.java
-
2chatto/src/main/resources/application.properties
-
13chatto/src/main/resources/templates/admin/home.html
-
7chatto/src/main/resources/templates/home.html
-
30chatto/src/main/resources/templates/login.html
-
10chatto/src/main/resources/templates/logout.html
-
17chatto/src/main/resources/templates/registration.html
-
13chatto/src/main/resources/templates/user/home.html
-
54chatto/src/test/java/org/ros/chatto/ChattoApplicationTests.java
@ -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,20 +1,28 @@ |
|||
package org.ros.chatto.controller; |
|||
|
|||
import org.ros.chatto.model.ChatUser; |
|||
import org.springframework.stereotype.Controller; |
|||
import org.springframework.web.bind.annotation.ModelAttribute; |
|||
import org.springframework.web.bind.annotation.PostMapping; |
|||
import org.springframework.web.bind.annotation.RequestMapping; |
|||
import org.springframework.web.servlet.ModelAndView; |
|||
import org.springframework.web.bind.annotation.GetMapping; |
|||
|
|||
@Controller |
|||
@RequestMapping("/login") |
|||
public class Login { |
|||
@PostMapping |
|||
public ModelAndView login(@ModelAttribute ChatUser chatUser) |
|||
@GetMapping("/login") |
|||
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"); |
|||
mv.addObject(chatUser); |
|||
return mv; |
|||
return "logout"; |
|||
} |
|||
} |
@ -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"; |
|||
} |
|||
} |
@ -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; |
|||
|
|||
import java.util.Date; |
|||
import java.util.HashSet; |
|||
import java.util.Set; |
|||
|
|||
import javax.persistence.CascadeType; |
|||
import javax.persistence.Column; |
|||
import javax.persistence.Entity; |
|||
import javax.persistence.EntityListeners; |
|||
import javax.persistence.GeneratedValue; |
|||
import javax.persistence.GenerationType; |
|||
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.Temporal; |
|||
import javax.persistence.TemporalType; |
|||
import javax.persistence.JoinColumn; |
|||
|
|||
import org.springframework.data.jpa.domain.support.AuditingEntityListener; |
|||
|
|||
import com.fasterxml.jackson.annotation.JsonBackReference; |
|||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
|||
|
|||
@Entity |
|||
@Table(name = "users") |
|||
@EntityListeners(AuditingEntityListener.class) |
|||
@JsonIgnoreProperties(value = {"password", "salt"}, |
|||
allowGetters = false) |
|||
@JsonIgnoreProperties(value = { "password"}, allowGetters = false) |
|||
|
|||
public class ChatUser { |
|||
@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; |
|||
@Column(name = "name") |
|||
private String userName; |
|||
String password, salt; |
|||
String password; |
|||
@Temporal(TemporalType.TIMESTAMP) |
|||
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() { |
|||
return userID; |
|||
} |
|||
|
|||
public void setUserID(int userID) { |
|||
this.userID = userID; |
|||
} |
|||
|
|||
|
|||
|
|||
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; |
|||
} |
|||
public String getSalt() { |
|||
return salt; |
|||
} |
|||
public void setSalt(String salt) { |
|||
this.salt = salt; |
|||
} |
|||
|
|||
public Date getJoinDate() { |
|||
return joinDate; |
|||
} |
|||
|
|||
public void setJoinDate(Date 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(); |
|||
this.userID = userID; |
|||
this.userName = userName; |
|||
this.password = password; |
|||
this.salt = salt; |
|||
this.joinDate = joinDate; |
|||
} |
|||
|
|||
public ChatUser() {} |
|||
|
|||
|
|||
|
|||
public ChatUser() { |
|||
} |
|||
|
|||
public Set<UserRole> getUserRoles() { |
|||
return userRoles; |
|||
} |
|||
|
|||
public void setUserRoles(Set<UserRole> userRoles) { |
|||
this.userRoles = userRoles; |
|||
} |
|||
|
|||
} |
@ -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; |
|||
} |
|||
|
|||
} |
@ -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; |
|||
} |
|||
|
|||
|
|||
} |
@ -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); |
|||
} |
@ -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); |
|||
} |
|||
|
|||
} |
@ -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); |
|||
} |
|||
|
|||
} |
@ -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> |
@ -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> |
@ -0,0 +1,10 @@ |
|||
<!DOCTYPE html> |
|||
<html> |
|||
<head> |
|||
<meta charset="UTF-8"> |
|||
<title>Insert title here</title> |
|||
</head> |
|||
<body> |
|||
|
|||
</body> |
|||
</html> |
@ -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> |
@ -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> |
Write
Preview
Loading…
Cancel
Save
Reference in new issue