Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. @Configuration
  2. @EnableWebSecurity
  3. @EnableGlobalMethodSecurity(prePostEnabled = true)
  4. public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  5.  
  6. @Autowired
  7. private JwtTokenDecoder jwtTokenDecoder;
  8.  
  9. @Override
  10. protected void configure(HttpSecurity http) throws Exception {
  11. http.csrf().disable()
  12. .httpBasic().disable()
  13. .formLogin().disable()
  14. .logout().disable()
  15. .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
  16. // Install the JWT authentication filter
  17. http.addFilterBefore(new JwtAuthenticationFilter(jwtTokenDecoder), BasicAuthenticationFilter.class);
  18. // Authorize only authenticated requests
  19. http.authorizeRequests()
  20. .anyRequest().authenticated();
  21. http.cors();
  22. }
  23. }
  24.  
  25. @Configuration
  26. @EnableWebMvc
  27. public class WebConfig implements WebMvcConfigurer {
  28.  
  29. @Override
  30. public void addCorsMappings(CorsRegistry registry) {
  31. registry.addMapping("/**")
  32. .allowedOrigins("*")
  33. .allowedMethods("*");
  34. }
  35. }
  36.  
  37. @RestController
  38. @RequestMapping("/admin")
  39. @Api("Administration API")
  40. @CrossOrigin(origins = "*")
  41. class AdminController {
  42.  
  43. @PostMapping("/user")
  44. @PreAuthorize("hasRole('Administrator')")
  45. public User createUser(@RequestBody String userJson,
  46. Authentication authentication) {
  47. EvidenzAuthentication evidenzAuthentication = (EvidenzAuthentication) authentication;
  48.  
  49. JsonObject dataAsJSON = new JsonParser().parse(userJson).getAsJsonObject();
  50. User u = new User();
  51. u.setFirstName((dataAsJSON.has("firstName") ? dataAsJSON.get("firstName").getAsString() : ""));
  52. u.setLastName((dataAsJSON.has("lastName") ? dataAsJSON.get("lastName").getAsString() : ""));
  53. u.setEmail((dataAsJSON.has("email") ? dataAsJSON.get("email").getAsString() : ""));
  54. u.setProfileId((dataAsJSON.has("profile") ? dataAsJSON.get("profile").getAsString() : ""));
  55. u.setIssuerId(evidenzAuthentication.getIssuerId());
  56.  
  57. if (userDao.createUser(u).isPresent()) {
  58. return userDao.createUser(u).get();
  59. } else {
  60. return null;
  61. }
  62. }
  63. }
  64.  
  65. axios.post('/admin/user',
  66. {data: "firstName":"Peter","lastName":"Sellers","email":"peter.sellers@party.com","profile":"Reader"},
  67. crossdomain: true,
  68. headers: { 'Content-Type': 'application/json',
  69. 'Cache-Control': 'no-cache',
  70. 'Authorization': 'Bearer ' + localStorage.getItem('auth_token') }})
  71. .then(response => {
  72. self.submitStatus = "OK";
  73. })
  74. .catch(function (error) {
  75. console.log(error)
  76. });;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement