iT邦幫忙

2024 iThome 鐵人賽

DAY 10
0

UserAuth

package com.example.puyuan_lun.AccountFountion.UserAuth;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

@Data
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "user_auth")
public class UserAuth implements UserDetails {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;

        @Column(nullable = false, length = 255)
        private String account;

        @Column(nullable = false, unique = true, length = 255)
        private String email;

        @Column(nullable = false, unique = true, length = 255)
        private String phone;

        @Column(nullable = false, length = 255)
        private String password;

        @Column(length = 100)
        private String fbId;

        @Column(length = 50)
        private String code;

        @Column(nullable = false, columnDefinition = "TINYINT(1) default 0")
        private Boolean verified;

        @Column(nullable = false, columnDefinition = "TINYINT(1) default 0")
        private Boolean privacyPolicy;

        @Column(nullable = false, columnDefinition = "TINYINT(1) default 0")
        private Boolean mustChangePassword;

        @Column(length = 100)
        private String fcmId;

        private Integer loginTimes;

        private Data createdAt;

        private Data updatedAt;

        @Enumerated(EnumType.STRING)
        private Role role;

        @Override
        public Collection<? extends GrantedAuthority> getAuthorities() {
                return List.of(new SimpleGrantedAuthority(role.name()));
        }

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

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

        @Override
        public boolean isAccountNonExpired() {
                return UserDetails.super.isAccountNonExpired();
        }

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

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

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


上一篇
Day 9 JWT 建構安全的 REST API
下一篇
Day 11 AuthenticationFilter
系列文
Spring Boot微服務架構:API設計與實現30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言