Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I am using spring Security in spring boot with angularjs : $http.post method from service i am not able to call UserDetailService class .
- security config .java
- package org.springernature.acdc.config;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springernature.acdc.security.AppUserDetailsServiceDAO;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
- import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
- import org.springframework.security.config.annotation.web.builders.HttpSecurity;
- import org.springframework.security.config.annotation.web.builders.WebSecurity;
- import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
- import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
- import org.springframework.security.web.access.AccessDeniedHandler;
- import org.springframework.security.web.authentication.AuthenticationFailureHandler;
- import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
- import org.springframework.security.web.authentication.logout.HttpStatusReturningLogoutSuccessHandler;
- @Configuration
- @EnableWebSecurity
- @EnableGlobalMethodSecurity(prePostEnabled = true)
- @ComponentScan(basePackages = { "org.springernature.acdc.*" })
- public class SecurityConfig extends WebSecurityConfigurerAdapter {
- private static final Logger logger = LoggerFactory.getLogger(SecurityConfig.class);
- public SecurityConfig() {
- super();
- System.out.println("In Security config .java Class");
- }
- @Autowired
- private AppUserDetailsServiceDAO appUserDetailsServiceDAO;
- /*
- * @Autowired private RestUnauthorizedEntryPoint
- * restAuthenticationEntryPoint;
- */
- @Autowired
- private AccessDeniedHandler restAccessDeniedHandler;
- @Autowired
- private AuthenticationSuccessHandler restAuthenticationSuccessHandler;
- @Autowired
- private AuthenticationFailureHandler restAuthenticationFailureHandler;
- /*
- * @Autowired private RememberMeServices rememberMeServices;
- */
- @Autowired
- public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
- auth.userDetailsService(appUserDetailsServiceDAO);
- }
- @Override
- public void configure(WebSecurity web) throws Exception {
- web.ignoring().antMatchers("/home.html", "/login.html", "/**", "/error/**");
- }
- @Override
- protected void configure(HttpSecurity http) throws Exception {
- http.authorizeRequests().antMatchers("/failure").permitAll()
- .antMatchers("/**").permitAll()
- .anyRequest()
- .authenticated()
- .and()
- /* .exceptionHandling()
- .authenticationEntryPoint(restAuthenticationEntryPoint)
- .accessDeniedHandler(restAccessDeniedHandler)
- .and()
- */ .formLogin()
- .loginProcessingUrl("/authenticate")
- .successHandler(restAuthenticationSuccessHandler)
- .failureHandler(restAuthenticationFailureHandler)
- .usernameParameter("username")
- .passwordParameter("password")
- .permitAll()
- .and()
- .logout()
- .logoutUrl("/logout")
- .logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler()).deleteCookies("JSESSIONID")
- .permitAll()
- .and();
- }
- }
- angularjs service js :-myapp.service('AuthSharedService', function($log, $http) {
- $log.log("hello sanket");
- this.login = function(employee) {
- console.log("login service")
- console.log(employee);
- $http(
- {
- method : 'POST',
- url : 'http://localhost:8080/authenticate',
- data : "userName=" + encodeURIComponent(employee.userName)
- + "&password="
- + encodeURIComponent(employee.password),
- headers : {
- 'Content-Type' : 'application/x-www-form-urlencoded'
- }
- }).then(function(response) {
- console.log("" + response)
- }).then(function(response) {
- console.log("" + response)
- });
- return null;
- }
- custom AppUserDetailsServiceDAO class
- import java.util.Collection;
- import java.util.List;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.springframework.security.core.GrantedAuthority;
- import org.springframework.security.core.authority.SimpleGrantedAuthority;
- import org.springframework.security.core.userdetails.UserDetails;
- import org.springframework.security.core.userdetails.UserDetailsService;
- import org.springframework.security.
- core.userdetails.UsernameNotFoundException;
- import org.springframework.stereotype.Component;
- @Component
- public class AppUserDetailsServiceDAO implements UserDetailsService {
- @Override
- public UserDetails loadUserByUsername(final String username) throws
- UsernameNotFoundException {
- System.out.println("in AppUserDetailsServiceDAO ");
- if (!username.equals("pankaj")) {
- throw new UsernameNotFoundException(username + " not found");
- }
- // creating dummy user details, should do JDBC operations
- return new UserDetails() {
- private static final long serialVersionUID = 2059202961588104658L;
- @Override
- public boolean isEnabled() {
- return true;
- }
- @Override
- public boolean isCredentialsNonExpired() {
- return true;
- }
- @Override
- public boolean isAccountNonLocked() {
- return true;
- }
- @Override
- public boolean isAccountNonExpired() {
- return true;
- }
- @Override
- public String getUsername() {
- return username;
- }
- @Override
- public String getPassword() {
- return "pankaj123";
- }
- @Override
- public Collection<? extends GrantedAuthority> getAuthorities() {
- List<SimpleGrantedAuthority> auths = new
- java.util.ArrayList<SimpleGrantedAuthority>();
- auths.add(new SimpleGrantedAuthority("Admin"));
- return auths;
- }
- };
- }
Add Comment
Please, Sign In to add comment