io.github.autsia.crowly.controllers.rest.AuthenticationController.java Source code

Java tutorial

Introduction

Here is the source code for io.github.autsia.crowly.controllers.rest.AuthenticationController.java

Source

/*
 * Copyright 2014 Dmytro Titov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.github.autsia.crowly.controllers.rest;

import io.github.autsia.crowly.model.CrowlyUser;
import io.github.autsia.crowly.security.CrowlyAuthenticationManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController("restAuthenticationController")
@RequestMapping("/rest/authentication")
public class AuthenticationController {

    private static final Logger logger = Logger.getLogger(AuthenticationController.class);

    private CrowlyAuthenticationManager authenticationManager;

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ResponseEntity<String> register(@RequestBody CrowlyUser user, HttpServletResponse response) {
        try {
            authenticationManager.addUser(user);
            return new ResponseEntity<>(HttpStatus.OK);
        } catch (Exception e) {
            logger.warn("User creation failed: " + e.getMessage());
            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
        }
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResponseEntity<String> login(@RequestBody CrowlyUser user, HttpServletResponse response) {
        try {
            Authentication request = new UsernamePasswordAuthenticationToken(user.getEmail(), user.getPassword());
            Authentication result = authenticationManager.authenticate(request);
            SecurityContextHolder.getContext().setAuthentication(result);
            return new ResponseEntity<>(HttpStatus.OK);
        } catch (AuthenticationException e) {
            logger.warn("Failed login attempt for username: " + e.getMessage());
            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
        }
    }

    @Autowired
    public void setAuthenticationManager(CrowlyAuthenticationManager authenticationManager) {
        this.authenticationManager = authenticationManager;
    }
}