aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/main/java/com/juick/server/security/JuickAuthenticationProvider.java
blob: bf0ed4d758f521b817bd247e4562db33f828da88 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.juick.server.security;

import com.juick.service.UserService;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import javax.inject.Inject;
import java.util.Collections;

/**
 * Created by vitalyster on 25.11.2016.
 */
public class JuickAuthenticationProvider implements AuthenticationProvider {
    @Inject
    UserService userService;
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        String name = authentication.getName();
        String password = authentication.getCredentials().toString();
        if (userService.checkPassword(name, password) > 0) {
            return new UsernamePasswordAuthenticationToken(name, password, Collections.singletonList(
                    new SimpleGrantedAuthority("ROLE_USER")
            ));
        }
        return null;
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return authentication.equals(UsernamePasswordAuthenticationToken.class);
    }
}