Skip to content

Commit ec0f9ae

Browse files
author
Riccardo Causo
committed
refactored authentication into a restcontroller
1 parent 161d27e commit ec0f9ae

File tree

3 files changed

+68
-61
lines changed

3 files changed

+68
-61
lines changed

src/main/java/com/myapp/auth/SecurityConfig.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ public SecurityConfig(UserService userService,
3232
this.tokenAuthenticationService = tokenAuthenticationService;
3333
}
3434

35+
@Bean
36+
public StatelessAuthenticationFilter authenticationTokenFilterBean() throws Exception {
37+
StatelessAuthenticationFilter authenticationTokenFilter = new StatelessAuthenticationFilter(tokenAuthenticationService);
38+
return authenticationTokenFilter;
39+
}
40+
3541
@Override
3642
protected void configure(HttpSecurity http) throws Exception {
3743
// we use jwt so that we can disable csrf protection
@@ -55,12 +61,7 @@ protected void configure(HttpSecurity http) throws Exception {
5561
.antMatchers(HttpMethod.GET, "/api/feed").hasRole("USER")
5662
;
5763

58-
http.addFilterBefore(
59-
new StatelessLoginFilter("/api/login", tokenAuthenticationService, userService, authenticationManager()),
60-
UsernamePasswordAuthenticationFilter.class);
61-
62-
http.addFilterBefore(
63-
new StatelessAuthenticationFilter(tokenAuthenticationService),
64+
http.addFilterBefore(authenticationTokenFilterBean(),
6465
UsernamePasswordAuthenticationFilter.class);
6566
}
6667

src/main/java/com/myapp/auth/StatelessLoginFilter.java

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.myapp.controller;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.myapp.auth.TokenHandler;
5+
import com.myapp.dto.UserParams;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.security.authentication.AuthenticationManager;
9+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
10+
import org.springframework.security.core.Authentication;
11+
import org.springframework.security.core.AuthenticationException;
12+
import org.springframework.security.core.context.SecurityContextHolder;
13+
import org.springframework.security.core.userdetails.UserDetails;
14+
import org.springframework.security.core.userdetails.UserDetailsService;
15+
import org.springframework.web.bind.annotation.RequestBody;
16+
import org.springframework.web.bind.annotation.RequestMapping;
17+
import org.springframework.web.bind.annotation.RequestMethod;
18+
import org.springframework.web.bind.annotation.RestController;
19+
20+
/**
21+
* Created by riccardo.causo on 17.10.2016.
22+
*/
23+
@RestController
24+
@RequestMapping("/api/login")
25+
public class AuthController {
26+
27+
private final AuthenticationManager authenticationManager;
28+
private final TokenHandler tokenHandler;
29+
private final UserDetailsService userDetailsService;
30+
31+
@Autowired
32+
AuthController(
33+
AuthenticationManager authenticationManager,
34+
TokenHandler tokenHandler,
35+
UserDetailsService userDetailsService
36+
){
37+
38+
this.authenticationManager = authenticationManager;
39+
this.tokenHandler = tokenHandler;
40+
this.userDetailsService = userDetailsService;
41+
}
42+
43+
44+
@RequestMapping(method = RequestMethod.POST)
45+
public ResponseEntity<String> authenticationRequest(@RequestBody UserParams params) throws AuthenticationException {
46+
47+
UsernamePasswordAuthenticationToken loginToken = params.toAuthenticationToken();
48+
Authentication authentication = authenticationManager.authenticate(loginToken);
49+
50+
SecurityContextHolder.getContext().setAuthentication(authentication);
51+
52+
UserDetails userDetails = userDetailsService.loadUserByUsername(params.getEmail().get());
53+
54+
String token = tokenHandler.createTokenForUser(userDetails);
55+
56+
// Return the token
57+
return ResponseEntity.ok().header("x-auth-token",token).body("");
58+
}
59+
60+
61+
}

0 commit comments

Comments
 (0)