Guest User

angularjs $http.post method

a guest
Mar 11th, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.97 KB | None | 0 0
  1. I am using spring Security in spring boot with angularjs :  $http.post method from service  i am not able to call UserDetailService class .
  2.  
  3. security config .java
  4. package org.springernature.acdc.config;
  5.  
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import org.springernature.acdc.security.AppUserDetailsServiceDAO;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.context.annotation.ComponentScan;
  11. import org.springframework.context.annotation.Configuration;
  12. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  13. import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
  14. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  15. import org.springframework.security.config.annotation.web.builders.WebSecurity;
  16. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  17. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  18. import org.springframework.security.web.access.AccessDeniedHandler;
  19. import org.springframework.security.web.authentication.AuthenticationFailureHandler;
  20. import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
  21. import org.springframework.security.web.authentication.logout.HttpStatusReturningLogoutSuccessHandler;
  22.  
  23. @Configuration
  24. @EnableWebSecurity
  25. @EnableGlobalMethodSecurity(prePostEnabled = true)
  26. @ComponentScan(basePackages = { "org.springernature.acdc.*" })
  27. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  28.  
  29.     private static final Logger logger = LoggerFactory.getLogger(SecurityConfig.class);
  30.  
  31.     public SecurityConfig() {
  32.         super();
  33.          System.out.println("In Security config .java Class");
  34.     }
  35.  
  36.     @Autowired
  37.     private AppUserDetailsServiceDAO appUserDetailsServiceDAO;
  38.  
  39.     /*
  40.      * @Autowired private RestUnauthorizedEntryPoint
  41.      * restAuthenticationEntryPoint;
  42.      */
  43.     @Autowired
  44.     private AccessDeniedHandler restAccessDeniedHandler;
  45.  
  46.     @Autowired
  47.     private AuthenticationSuccessHandler restAuthenticationSuccessHandler;
  48.  
  49.     @Autowired
  50.     private AuthenticationFailureHandler restAuthenticationFailureHandler;
  51.  
  52.     /*
  53.      * @Autowired private RememberMeServices rememberMeServices;
  54.      */
  55.     @Autowired
  56.     public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
  57.         auth.userDetailsService(appUserDetailsServiceDAO);
  58.     }
  59.  
  60.     @Override
  61.     public void configure(WebSecurity web) throws Exception {
  62.         web.ignoring().antMatchers("/home.html", "/login.html", "/**", "/error/**");
  63.     }
  64.  
  65.     @Override
  66.     protected void configure(HttpSecurity http) throws Exception {
  67.         http.authorizeRequests().antMatchers("/failure").permitAll()
  68.         .antMatchers("/**").permitAll()
  69.          .anyRequest()
  70.         .authenticated()
  71.         .and()
  72. /*      .exceptionHandling()
  73.         .authenticationEntryPoint(restAuthenticationEntryPoint)
  74.         .accessDeniedHandler(restAccessDeniedHandler)
  75.         .and()
  76. */      .formLogin()
  77.         .loginProcessingUrl("/authenticate")
  78.         .successHandler(restAuthenticationSuccessHandler)
  79.         .failureHandler(restAuthenticationFailureHandler)
  80.         .usernameParameter("username")
  81.         .passwordParameter("password")
  82.         .permitAll()
  83.         .and()
  84.         .logout()
  85.         .logoutUrl("/logout")
  86.         .logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler()).deleteCookies("JSESSIONID")
  87.         .permitAll()
  88.         .and();
  89.     }
  90. }
  91.  
  92. angularjs service js :-myapp.service('AuthSharedService', function($log, $http) {
  93.     $log.log("hello sanket");
  94.     this.login = function(employee) {
  95.         console.log("login service")
  96.         console.log(employee);
  97.  
  98.         $http(
  99.                 {
  100.                     method : 'POST',
  101.                     url : 'http://localhost:8080/authenticate',
  102.                     data : "userName=" + encodeURIComponent(employee.userName)
  103.                             + "&password="
  104.                             + encodeURIComponent(employee.password),
  105.                     headers : {
  106.                         'Content-Type' : 'application/x-www-form-urlencoded'
  107.                     }
  108.                 }).then(function(response) {
  109.             console.log("" + response)
  110.         }).then(function(response) {
  111.             console.log("" + response)
  112.         });
  113.         return null;
  114.     }
  115.  
  116.    custom AppUserDetailsServiceDAO class
  117.  
  118.  
  119.     import java.util.Collection;
  120.     import java.util.List;
  121.  
  122.     import org.apache.commons.logging.Log;
  123.     import org.apache.commons.logging.LogFactory;
  124.     import org.springframework.security.core.GrantedAuthority;
  125.     import org.springframework.security.core.authority.SimpleGrantedAuthority;
  126.     import org.springframework.security.core.userdetails.UserDetails;
  127.     import org.springframework.security.core.userdetails.UserDetailsService;
  128.     import org.springframework.security.
  129.     core.userdetails.UsernameNotFoundException;
  130.     import org.springframework.stereotype.Component;
  131.  
  132.     @Component
  133.     public class AppUserDetailsServiceDAO implements UserDetailsService {
  134.  
  135.     @Override
  136.     public UserDetails loadUserByUsername(final String username) throws    
  137.     UsernameNotFoundException {
  138.         System.out.println("in AppUserDetailsServiceDAO ");
  139.  
  140.         if (!username.equals("pankaj")) {
  141.             throw new UsernameNotFoundException(username + " not found");
  142.         }
  143.  
  144.         // creating dummy user details, should do JDBC operations
  145.         return new UserDetails() {
  146.  
  147.             private static final long serialVersionUID = 2059202961588104658L;
  148.  
  149.             @Override
  150.             public boolean isEnabled() {
  151.                 return true;
  152.             }
  153.  
  154.             @Override
  155.             public boolean isCredentialsNonExpired() {
  156.                 return true;
  157.             }
  158.  
  159.             @Override
  160.             public boolean isAccountNonLocked() {
  161.                 return true;
  162.             }
  163.  
  164.             @Override
  165.             public boolean isAccountNonExpired() {
  166.                 return true;
  167.             }
  168.  
  169.             @Override
  170.             public String getUsername() {
  171.                 return username;
  172.             }
  173.  
  174.             @Override
  175.             public String getPassword() {
  176.                 return "pankaj123";
  177.             }
  178.  
  179.             @Override
  180.             public Collection<? extends GrantedAuthority> getAuthorities() {
  181.                 List<SimpleGrantedAuthority> auths = new    
  182.        java.util.ArrayList<SimpleGrantedAuthority>();
  183.                 auths.add(new SimpleGrantedAuthority("Admin"));
  184.                 return auths;
  185.             }
  186.         };
  187.     }
Add Comment
Please, Sign In to add comment