Hướng dẫn về Spring Security và JWT Phần 1

  • Ở bài viết này mình sẽ hướng dẫn mọi người cách sử dụng Spring Security
  • Trước hết chúng ta sẽ tìm hiểu JWT là gì? JWT là viết tắt của từ Json Web Token là một chuỗi mã hóa được gửi kèm trong Header của client request có tác dụng giúp phía server xác thực request người dùng có hợp lệ hay không. Được sử dụng phổ biến trong các hệ thống API ngày nay.
  • Cấu hình: chúng ta sẽ thêm các thư viện sau:
  • Đầu tiên chúng ta sẽ tạo ra class User, Role và UserPrinciple ở đây mình sử dụng Lombok với @Data để tự tạo get, set
  • Tạo class User như sau:
  • Tạo class Role:
  • Tạo class UserPrinciple:

package com.codegym.demo.model;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class UserPrinciple implements UserDetails {

    private static final long serialVersionUID = 1L;

    private Long id;

    private String username;

    private String password;

    private Collection<? extends GrantedAuthority> roles;

    public UserPrinciple(Long id,
                         String username, String password,
                         Collection<? extends GrantedAuthority> roles) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.roles = roles;
    }

    public static UserPrinciple build(User user) {
        List<GrantedAuthority> authorities = user.getRoles().stream().map(role ->
                new SimpleGrantedAuthority(role.getName())
        ).collect(Collectors.toList());

        return new UserPrinciple(
                user.getId(),
                user.getUsername(),
                user.getPassword(),
                authorities
        );
    }

    public Long getId() {
        return id;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return roles;
    }


    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        UserPrinciple user = (UserPrinciple) o;
        return Objects.equals(id, user.id);
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }
}

  • Sau khi tạo xong 3 model trên ta sẽ tạo 2 repository có tên là UserRepository và RoleRepository như sau:
  • Tiến hành tạo Service:

UserService

UserServiceImpl

RoleService

RoleServiceImpl

3 comments On Hướng dẫn về Spring Security và JWT Phần 1

Leave a reply:

Your email address will not be published.

Site Footer

Sliding Sidebar

Facebook