2
2
3
3
import com .auth0 .jwt .JWT ;
4
4
import api .restful .model .user .AuthUser ;
5
+ import api .restful .model .views .UserCache ;
6
+ import api .restful .model .user .Authorization ;
7
+ import api .restful .model .user .AuthorizationRepository ;
5
8
import api .restful .model .views .ResponseToken ;
9
+ import api .restful .services .AuthorizationServiceImpl ;
10
+
6
11
import com .google .gson .Gson ;
7
12
import com .fasterxml .jackson .databind .ObjectMapper ;
8
13
import org .springframework .security .authentication .AuthenticationManager ;
12
17
import org .springframework .security .core .userdetails .User ;
13
18
import org .springframework .security .web .authentication .UsernamePasswordAuthenticationFilter ;
14
19
20
+ import org .springframework .security .core .GrantedAuthority ;
21
+ import org .springframework .security .core .authority .SimpleGrantedAuthority ;
22
+
15
23
import javax .servlet .FilterChain ;
16
24
import javax .servlet .ServletException ;
17
25
import javax .servlet .http .HttpServletRequest ;
20
28
import java .io .PrintWriter ;
21
29
import java .util .ArrayList ;
22
30
import java .util .Date ;
31
+ import java .util .List ;
32
+ import java .util .Collection ;
23
33
24
34
import static com .auth0 .jwt .algorithms .Algorithm .HMAC512 ;
25
35
import static api .restful .controller .security .SecurityConstants .EXPIRATION_TIME ;
@@ -37,11 +47,13 @@ public JWTAuthenticationFilter(AuthenticationManager authenticationManager) {
37
47
public Authentication attemptAuthentication (HttpServletRequest req , HttpServletResponse res ) throws AuthenticationException {
38
48
try {
39
49
AuthUser creds = new ObjectMapper ().readValue (req .getInputStream (), AuthUser .class );
50
+ List <GrantedAuthority > updatedAuthorities = new ArrayList <>();
51
+ updatedAuthorities .add (new SimpleGrantedAuthority ("ROLE_USER" ));
40
52
return authenticationManager .authenticate (
41
53
new UsernamePasswordAuthenticationToken (
42
54
creds .getUsername (),
43
55
creds .getPassword (),
44
- new ArrayList <>()
56
+ updatedAuthorities
45
57
)
46
58
);
47
59
} catch (IOException e ) {
@@ -51,11 +63,20 @@ public Authentication attemptAuthentication(HttpServletRequest req, HttpServletR
51
63
52
64
@ Override
53
65
protected void successfulAuthentication (HttpServletRequest req , HttpServletResponse res , FilterChain chain , Authentication auth ) throws IOException , ServletException {
66
+ List <Authorization > authorizations = (List <Authorization >) auth .getAuthorities ();
67
+ List <String > aut = new ArrayList <String >();
68
+ for (Authorization autho : authorizations ) {
69
+ aut .add (autho .getAuthority ());
70
+ }
71
+ String username = ((User ) auth .getPrincipal ()).getUsername ();
72
+ String password = ((User ) auth .getPrincipal ()).getPassword ();
73
+ UserCache user = new UserCache (username , password , aut );
74
+ String jsonUser = new Gson ().toJson (user );
54
75
String token = JWT .create ()
55
- .withSubject ((( User ) auth . getPrincipal ()). getUsername () )
76
+ .withSubject (jsonUser )
56
77
.withExpiresAt (new Date (System .currentTimeMillis () + EXPIRATION_TIME ))
57
78
.sign (HMAC512 (SECRET .getBytes ()));
58
- ResponseToken response = new ResponseToken (200 , token , true , "Use this token to API CRUD options" );
79
+ ResponseToken response = new ResponseToken (200 , token , user , "Use this token to API CRUD options" );
59
80
String jsonString = new Gson ().toJson (response );
60
81
PrintWriter out = res .getWriter ();
61
82
res .addHeader (HEADER_STRING , token );
0 commit comments