Somthing about Spring Boot

Posted seliote

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Somthing about Spring Boot相关的知识,希望对你有一定的参考价值。

Auditor

@Configuration
public class CommonConfiguration implements AuditorAware<String> {

    /**
     * Get current auditor
     * @return Auditor Optional object
     */
    @Override
    public Optional<String> getCurrentAuditor() {
        //Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        //if (authentication == null) {
        //    return Optional.of("Unknown");
        //} else if (!authentication.isAuthenticated()) {
        //    return Optional.of("Unauthenticated");
        //} else {
        //    UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        //    return Optional.of(userDetails.getUsername());
        //}
        return Optional.of("system");
    }
}

Fucking Lombok...

/**
 * For database auditing
 * Could not audit remove operating
 *
 * @author seliote
 * @since 2019-10-19
 */
// Designates its child class applied the mapping info, and had no separate table defined for it
@MappedSuperclass
// Enable auto @PrePersist & @PreUpdate
@EntityListeners(AuditingEntityListener.class)
// Hibernate change delete sql before execute, there could not get table name
public class Auditable {

    // Row create user
    @CreatedBy
    @Column(name = "create_user")
    private String createUser;

    // Row create date
    @CreatedDate
    @Column(name = "create_date")
    private LocalDateTime createDate;

    // Row modify user
    @LastModifiedBy
    @Column(name = "modify_user")
    private String modifyUser;

    // Row modify date
    @LastModifiedDate
    @Column(name = "modify_date")
    private LocalDateTime modifyDate;

    // Delete mark, this field could auto audit, should manual update in every entity
    @Column(name = "delete_mark")
    private boolean deleteMark;

    public String getCreateUser() {
        return createUser;
    }

    public void setCreateUser(String createUser) {
        this.createUser = createUser;
    }

    public LocalDateTime getCreateDate() {
        return createDate;
    }

    public void setCreateDate(LocalDateTime createDate) {
        this.createDate = createDate;
    }

    public String getModifyUser() {
        return modifyUser;
    }

    public void setModifyUser(String modifyUser) {
        this.modifyUser = modifyUser;
    }

    public LocalDateTime getModifyDate() {
        return modifyDate;
    }

    public void setModifyDate(LocalDateTime modifyDate) {
        this.modifyDate = modifyDate;
    }

    public boolean isDeleteMark() {
        return deleteMark;
    }

    public void setDeleteMark(boolean deleteMark) {
        this.deleteMark = deleteMark;
    }
}

JPA annotation should in FIELD not GETTER, otherwise the method extended named get... will also mapping to database column

/**
 * Admin entity
 *
 * @author seliote
 * @since 2019-10-19
 */
@Entity
@Table(name = "admin")
@Where(clause = "delete_mark = 0")
@SQLDelete(sql = "UPDATE `admin` SET `delete_mark` = 1 WHERE `uuid` = ?")
public class AdminEntity extends Auditable implements UserDetails {

    // Admin uuid, primary key
    @Id
    @Column(name = "uuid")
    private String uuid;

    // Admin name, login name
    @Column(name = "user")
    private String user;

    // Admin password, login password
    @Column(name = "password")
    private String password;

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

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

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // Grant ADMIN authority
        return Arrays.asList(new SimpleGrantedAuthority(Constant.Security.Authority.ADMIN));
    }

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

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

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

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

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

Property

This line defualt in application.properties, otherwise need @PropertySource

com.seliote.productId=666

Could inject this component to other bean and use getter

/**
 * Inject all properties
 *
 * @author seliote
 * @since 2019-10-20
 */
@Component
@ConfigurationProperties(prefix = "com.seliote")
public class PropertyConfiguration {
    private String productId;

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }
}

HTTPS

server:
  # for embed Tomcat...
  port: 8443
  ssl:
    key-store: classpath:cert/demo.jks
    key-password: drowssap321
    key-store-password: drowssap123

以上是关于Somthing about Spring Boot的主要内容,如果未能解决你的问题,请参考以下文章

spring-boot实战05:Spring Boo多环境配置及配置属性注入到对象

spring boo的简单搭建(eclipse+springboot + redis + mysql + thymeleaf)

The problem: somthing wrong when my computer excute the command "git clone XXXX"

some notes about spring aop

Very important notes about Spring @Transnational(Srping事务注解 @Transnational重要注意事项)

about study