0

This is my login controller:

app.controller('loginCtrl', function($scope,$http,$state,$location,$q) {

      var self = this;
        self.user={uname:'',password:''};
        self.users=[];
    this.postForm=function(user)
    {
         var deferred = $q.defer();
            $http.post('http://localhost:8080/HB2/login/', user)
                .then(
                function (response) {
                    deferred.resolve(response.data);
                    console.log("Success");
                },
                function(errResponse){
                    console.error('Invalid Username/Password');
                    deferred.reject(errResponse);
                }
            );
            return deferred.promise;
        };

and this is my rest login api:

@RequestMapping(value ="/login/", method = RequestMethod.POST,produces= MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Void> loginUser(@RequestBody loginModel user,    UriComponentsBuilder ucBuilder) {


        if ((user.getUname().equals("kartik"))&&(user.getPassword().equals("gogia"))) {

            return new ResponseEntity<Void>(HttpStatus.OK);
        }
        else
        return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
    }

Help me how to do login and to transfer to the home page.

Marvin Fischer
  • 2,552
  • 3
  • 23
  • 34

2 Answers2

1

You can used the login form as Rest Url like as:

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .formLogin()
                .loginProcessingUrl("/app/authentication/oauth")
                .usernameParameter("j_username")
                .passwordParameter("j_password")
                .permitAll()
                .and()
            .logout()
                .logoutUrl("/app/logout")
                .deleteCookies("JSESSIONID")
                .permitAll()
                .and()
            .csrf()
                .disable()
            .authorizeRequests()
                .antMatchers("/resources/**").permitAll()
                .antMatchers("/api/**").authenticated();
    }

Now when you can request from angular js controller to /app/authentication/oauth with j_username and j_password then it will automatically goes to you ApplicationDetailService.

Charnjeet Singh
  • 3,056
  • 6
  • 35
  • 65
  • I am reaching out many to get some help. Can you please see my question and see if you help me. Thanks in advance - Sudhir. https://stackoverflow.com/questions/50064389/angular-4-spring-boot-spring-security-getting-browser-authentication-popup – SK. Apr 28 '18 at 17:59
  • and https://stackoverflow.com/questions/50076941/angular-4-0-spring-boot-spring-security-templateinputexception-error-resol – SK. Apr 28 '18 at 18:00
0

You can try below code

    @RequestMapping(value="/login", method=RequestMethod.POST)
    public ModelAndView loginUser(@RequestBody loginModel user,  HttpServletRequest request, HttpServletResponse response){   
        String returnView = "home";//home.html

        try{    
            //try validating user..throw excception if user doesn't have access..                       
        }catch (InvalidUserException e){
            returnView = "error";//error.html
        }       
        ModelAndView mv = new ModelAndView(returnView);
        return mv;
    } 
ngCoder
  • 2,095
  • 1
  • 13
  • 22
  • I am reaching out many to get some help. Can you please see my question and see if you help me. Thanks in advance - Sudhir. https://stackoverflow.com/questions/50064389/angular-4-spring-boot-spring-security-getting-browser-authentication-popup – SK. Apr 28 '18 at 17:56
  • and https://stackoverflow.com/questions/50076941/angular-4-0-spring-boot-spring-security-templateinputexception-error-resol – SK. Apr 28 '18 at 17:57