Advertisement
codeuniv

Тестовое задание (OAuth, REST, Spring Boot) - CODE

Apr 5th, 2022 (edited)
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.39 KB | None | 0 0
  1. request to REST Service
  2. User-Agent: MyApp/1.0 (my-app-feedback@example.com)
  3.  
  4. @Override
  5.         protected void configure(HttpSecurity http) throws Exception {
  6.             http
  7.                     .formLogin()
  8.                     .and()
  9.                     .httpBasic().disable()
  10.                     .anonymous().disable()
  11.                     .authorizeRequests().anyRequest().authenticated()            
  12.         }
  13.     }
  14.  
  15.  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  16.             log.info("Defining inMemoryAuthentication (2 users)");
  17.             auth
  18.                     .inMemoryAuthentication()
  19.                     .withUser("user").password("password")
  20.                     .roles("USER")
  21.                     .and()
  22.                     .withUser("admin").password("password")
  23.                     .roles("USER", "ADMIN")            
  24.         }
  25.  
  26. @Override
  27.     protected void configure(final HttpSecurity http) throws Exception {
  28.         http.authorizeRequests()
  29.             .antMatchers("/anonymous*").anonymous()
  30.             .antMatchers("/login*").permitAll()
  31.             .anyRequest().authenticated()
  32.            
  33.             .and()
  34.             .formLogin()
  35.             .loginPage("/login.html")
  36.             .loginProcessingUrl("/login")
  37.             .failureUrl("/login.html?error=true")
  38.            
  39.             .and()
  40.             .logout().deleteCookies("JSESSIONID")
  41.            
  42.             .and()
  43.             .rememberMe().key("uniqueAndSecret")
  44.             ;
  45.     }
  46.  
  47.  
  48.  
  49. @Controller
  50. public class MainController {
  51.  
  52.   ...
  53.  
  54.   // Login form
  55.   @RequestMapping("/login.html")
  56.   public String login() {
  57.     return "login.html";
  58.   }
  59.  
  60.   // Login form with error
  61.   @RequestMapping("/login-error.html")
  62.   public String loginError(Model model) {
  63.     model.addAttribute("loginError", true);
  64.     return "login.html";
  65.   }
  66.  
  67. }
  68.  
  69.  
  70. @RequestMapping("/messages/inbox")
  71.     public ModelAndView findMessagesForUser(CsrfToken token) {
  72.          SecurityContext context = SecurityContextHolder.getContext();
  73.          Authentication authentication = context.getAuthentication();
  74.  
  75.          if (authentication != null && authentication.getPrincipal() instanceof UserDetails) {
  76.              CustomUser customUser = (CustomUser) authentication.getPrincipal();
  77.              // .. find messages for this user and return them ...
  78.          }
  79.  
  80.          // todo
  81.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement