Advertisement
Guest User

Untitled

a guest
May 17th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.20 KB | None | 0 0
  1. HTML :
  2.  
  3. <form role="form" ng-submit="controller.login()">
  4.  
  5.     <div class="form-group">
  6.         <label for="username">Username:</label>
  7.         <input type="text" class="form-control" id="username" name="username" ng-model="controller.credentials.username"/>
  8.     </div>
  9.    
  10.     <div class="form-group">
  11.         <label for="password">Password:</label>
  12.         <input type="password" class="form-control" id="password" name="password" ng-model="controller.credentials.password"/>
  13.     </div>
  14.    
  15.     <button type="submit" class="btn btn-primary">Submit</button>
  16.    
  17. </form>
  18.  
  19. --------------------------------------------------
  20. JS :
  21.  
  22. myModule.controller('NavCtrl',function($rootScope, $location, $http){
  23.  
  24.     var self = this
  25.  
  26.     var authenticate = function(credentials, callback) {
  27.        
  28.         var formulaireLoginDto = credentials ? {'login':credentials.username,'password':credentials.password} : {};
  29.        
  30.         $http.post('check-login', formulaireLoginDto).then(
  31.             function(response) {
  32.                 if (response.data.name) {
  33.                     $rootScope.authenticated = true;
  34.                 } else {
  35.                     $rootScope.authenticated = false;
  36.                 }
  37.                 callback && callback();
  38.             },
  39.             function() {
  40.                 $rootScope.authenticated = false;
  41.                 callback && callback();
  42.             }
  43.         );
  44.        
  45.     }
  46.  
  47.     authenticate();
  48.     self.credentials = {};
  49.    
  50.     self.login = function() {
  51.         authenticate(self.credentials, function() {
  52.             if ($rootScope.authenticated) {
  53.                 $location.path("/");
  54.                 self.error = false;
  55.             } else {
  56.                 $location.path("/login");
  57.                 self.error = true;
  58.             }
  59.         });
  60.     };
  61.  
  62.    
  63. });
  64.  
  65. -------------------------- JAVA SPRING SECURITY + SPRING BOOT
  66.  
  67. @SpringBootApplication
  68. public class BusinessBootApplication {
  69.  
  70.     public static void main(String[] args) {
  71.         SpringApplication.run(BusinessBootApplication.class, args);
  72.     }
  73.    
  74.     @Configuration
  75.     @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
  76.     protected static class SecurityConfiguration extends WebSecurityConfigurerAdapter{
  77.         @Override
  78.         protected void configure(HttpSecurity http) throws Exception {
  79.             http
  80.             .httpBasic()
  81.             .and()
  82.             .authorizeRequests()
  83.             .antMatchers("/index.html","/home.html","/login.html","/").permitAll()
  84.             .anyRequest()
  85.             .authenticated()
  86.             .and()
  87.             .addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class)
  88.             .csrf()
  89.             .csrfTokenRepository(csrfTokenRepository());
  90.         }
  91.        
  92.         private CsrfTokenRepository csrfTokenRepository() {
  93.             HttpSessionCsrfTokenRepository repo = new HttpSessionCsrfTokenRepository();
  94.             repo.setHeaderName("X-XSRF-TOKEN");
  95.             return repo;
  96.         }
  97.     }
  98. }
  99.  
  100. -------------------------------- JAVA My REST Webservice
  101.  
  102. @RestController
  103. public class LoginRest {
  104.  
  105.     @Autowired
  106.     private UserDao userDao;
  107.    
  108.     @RequestMapping("/check-login")
  109.     public User login(FormulaireLoginDto formulaireLoginDto){
  110.         User user = null;
  111.        
  112.         try {
  113.             user = userDao.findByLogin(formulaireLoginDto.getLogin());
  114.         } catch (Exception e) {
  115.             return null;
  116.         }
  117.        
  118.         return user;
  119.     }
  120.    
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement