Advertisement
Guest User

Untitled

a guest
Mar 9th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.41 KB | None | 0 0
  1. package com.innaun.model;
  2.  
  3. import org.springframework.data.rest.core.annotation.RestResource;
  4.  
  5. import javax.persistence.*;
  6. import javax.validation.constraints.NotNull;
  7. import java.util.HashSet;
  8. import java.util.Set;
  9.  
  10. @Entity
  11. public class User {
  12.  
  13. @Id
  14. @GeneratedValue(strategy = GenerationType.AUTO)
  15. private Long userId;
  16.  
  17. @Column(unique = true, nullable = false)
  18. private String username;
  19.  
  20. @NotNull
  21. @RestResource(exported = false )
  22. private String password;
  23.  
  24. @NotNull
  25. private boolean enabled;
  26.  
  27. @OneToMany
  28. private Set<UserRole> userRoles = new HashSet<UserRole>(0);
  29.  
  30. public User() {
  31. }
  32.  
  33. public User(String username, String password, boolean enabled) {
  34. this.username = username;
  35. this.password = password;
  36. this.enabled = enabled;
  37. }
  38.  
  39. public Set<UserRole> getUserRoles() {
  40. return userRoles;
  41. }
  42.  
  43. public void setUserRoles(Set<UserRole> userRoles) {
  44. this.userRoles = userRoles;
  45. }
  46.  
  47. public Long getUserId() {
  48. return userId;
  49. }
  50.  
  51. public void setUserId(Long userId) {
  52. this.userId = userId;
  53. }
  54.  
  55. public String getUsername() {
  56. return username;
  57. }
  58.  
  59. public void setUsername(String username) {
  60. this.username = username;
  61. }
  62.  
  63. public String getPassword() {
  64. return password;
  65. }
  66.  
  67. public void setPassword(String password) {
  68. this.password = password;
  69. }
  70.  
  71. public boolean isEnabled() {
  72. return enabled;
  73. }
  74.  
  75. public void setEnabled(boolean enabled) {
  76. this.enabled = enabled;
  77. }
  78. }
  79.  
  80. package com.innaun.model;
  81.  
  82. import javax.persistence.*;
  83. import javax.validation.constraints.NotNull;
  84.  
  85. @Entity
  86. public class UserRole {
  87.  
  88. @Id
  89. @GeneratedValue(strategy = GenerationType.AUTO)
  90. private Long userRoleId;
  91.  
  92. @NotNull
  93. private String userRole;
  94.  
  95. @ManyToOne
  96. private User user;
  97.  
  98. public UserRole() {
  99. }
  100.  
  101. public UserRole(String userRole, User user) {
  102. this.userRole = userRole;
  103. this.user = user;
  104. }
  105.  
  106. public Long getUserRoleId() {
  107. return userRoleId;
  108. }
  109.  
  110. public void setUserRoleId(Long userRoleId) {
  111. this.userRoleId = userRoleId;
  112. }
  113.  
  114. public String getUserRole() {
  115. return userRole;
  116. }
  117.  
  118. public void setUserRole(String userRole) {
  119. this.userRole = userRole;
  120. }
  121.  
  122. public User getUser() {
  123. return user;
  124. }
  125.  
  126. public void setUser(User user) {
  127. this.user = user;
  128. }
  129. }
  130.  
  131. package com.innaun.model;
  132.  
  133. import org.springframework.data.repository.CrudRepository;
  134. import org.springframework.data.repository.query.Param;
  135. import org.springframework.data.rest.core.annotation.RepositoryRestResource;
  136.  
  137. @RepositoryRestResource
  138. public interface UserRepository extends CrudRepository<User, Long>{
  139. User findByUsername(@Param("user") String user);
  140. }
  141.  
  142. package com.innaun.model;
  143.  
  144. import org.springframework.data.repository.CrudRepository;
  145. import org.springframework.data.rest.core.annotation.RepositoryRestResource;
  146.  
  147. @RepositoryRestResource
  148. public interface UserRoleRepository extends CrudRepository<UserRole, Long> {
  149. }
  150.  
  151. package com.innaun.model;
  152.  
  153. import com.innaun.model.UserRepository;
  154. import com.innaun.model.UserRole;
  155. import org.springframework.security.core.GrantedAuthority;
  156. import org.springframework.security.core.authority.SimpleGrantedAuthority;
  157. import org.springframework.security.core.userdetails.User;
  158. import org.springframework.beans.factory.annotation.Autowired;
  159. import org.springframework.security.core.userdetails.UserDetails;
  160. import org.springframework.security.core.userdetails.UserDetailsService;
  161. import org.springframework.security.core.userdetails.UsernameNotFoundException;
  162. import org.springframework.stereotype.Service;
  163.  
  164. import javax.transaction.Transactional;
  165. import java.util.ArrayList;
  166. import java.util.HashSet;
  167. import java.util.List;
  168. import java.util.Set;
  169.  
  170. @Service("appUserDetailsService")
  171. public class AppUserDetailsService implements UserDetailsService {
  172.  
  173. @Autowired
  174. private UserRepository userRepository;
  175.  
  176.  
  177. @Transactional
  178. @Override
  179. public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
  180. com.innaun.model.User user = userRepository.findByUsername(s);
  181.  
  182. List<GrantedAuthority> authorities = buildUserAuthority(user.getUserRoles());
  183.  
  184. return buildUserForAuthentication(user, authorities);
  185. }
  186.  
  187. private User buildUserForAuthentication(com.innaun.model.User user, List<GrantedAuthority> authorities){
  188. return new User(user.getUsername(), user.getPassword(), user.isEnabled(), true, true, true, authorities);
  189. }
  190.  
  191. private List<GrantedAuthority> buildUserAuthority(Set<UserRole> userRoles){
  192. Set<GrantedAuthority> setAuths = new HashSet<GrantedAuthority>();
  193.  
  194. for (UserRole userRole : userRoles){
  195. setAuths.add(new SimpleGrantedAuthority(userRole.getUserRole()));
  196. }
  197.  
  198. List<GrantedAuthority> result = new ArrayList<GrantedAuthority>(setAuths);
  199.  
  200. return result;
  201. }
  202.  
  203.  
  204. }
  205.  
  206. package com.innaun;
  207.  
  208. import org.springframework.beans.factory.annotation.Autowired;
  209. import org.springframework.beans.factory.annotation.Qualifier;
  210. import org.springframework.context.annotation.Bean;
  211. import org.springframework.context.annotation.Configuration;
  212. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  213. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  214. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  215. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  216. import org.springframework.security.core.userdetails.UserDetailsService;
  217. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  218. import org.springframework.security.crypto.password.PasswordEncoder;
  219.  
  220.  
  221. @Configuration
  222. @EnableWebSecurity
  223. public class ApplicationRESTSecurity extends WebSecurityConfigurerAdapter {
  224.  
  225. @Qualifier("appUserDetailsService")
  226. @Autowired
  227. UserDetailsService userDetailsService;
  228.  
  229. @Autowired
  230. public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception{
  231. auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
  232. }
  233.  
  234. @Bean
  235. public PasswordEncoder passwordEncoder() {
  236. PasswordEncoder encoder = new BCryptPasswordEncoder();
  237. return encoder;
  238. }
  239.  
  240. @Override
  241. protected void configure(HttpSecurity http) throws Exception {
  242. http
  243. .authorizeRequests()
  244. .anyRequest().fullyAuthenticated()
  245. .and().httpBasic()
  246. .and().csrf()
  247. .disable();
  248. }
  249. }
  250.  
  251. package com.innaun;
  252.  
  253. import com.innaun.model.User;
  254. import com.innaun.model.UserRepository;
  255. import org.springframework.boot.CommandLineRunner;
  256. import org.springframework.boot.SpringApplication;
  257. import org.springframework.boot.autoconfigure.SpringBootApplication;
  258. import org.springframework.context.annotation.Bean;
  259.  
  260. @SpringBootApplication
  261. public class PitchuApplication {
  262.  
  263. public static void main(String[] args) {
  264. SpringApplication.run(PitchuApplication.class, args);
  265. }
  266.  
  267. @Bean
  268. CommandLineRunner init(UserRepository userRepository) {
  269.  
  270. return (args) -> {
  271. userRepository.save(new User("myuser", "mypassword", true));
  272. };
  273. }
  274.  
  275. }
  276.  
  277. curl -u myuser:mypassword localhost:8080
  278.  
  279. {"timestamp":1489090315435,"status":401,"error":"Unauthorized","message":"Bad credentials","path":"/"}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement