2016-03-07 70 views
0

我有一個vaadin maven項目,我在spring security中集成,所以我能夠看到spring security的默認登錄形式。 現在我想從postgres數據庫獲得用戶請幫助我嗎?你有任何tuto或書嗎?感謝「SA很多Spring Security with vaadin maven project中的數據庫

+0

我有一個應用程序的工作示例使用Vaadin + Spring Boot + Spring Security,但不幸的是這個例子沒有任何文檔。但是,也許你可以從你的任務的代碼中獲得一些啓發:https://github.com/rolandkrueger/vaadin-by-example/tree/master/en/architecture/SpringBootSecurity –

+0

謝謝,我將會看到它 –

+0

請RolandKrüger,這個例子中數據庫的配置在哪裏? –

回答

0

作爲例子(尊重羅蘭Krügers例子)春季啓動應用+ SpringSecurity + Vaadin4Spring#Hibernate,就必須在application.properties定義以下屬性:

# DATASOURCE 
spring.datasource.url=jdbc:postgresql://localhost/myDB 
spring.datasource.username=myuser 
spring.datasource.password=mypwd 
spring.datasource.driverClassName=org.postgresql.Driver 
spring.datasource.xa.data-source-class-name=org.postgresql.xa.PGXADataSource 
spring.datasource.pinGlobalTxToPhysicalConnection="true" 

# one transaction manager via view 
spring.jpa.open-in-view=true 
# for mapping models and attributes to table names and column names 
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy 
spring.jta.default-transaction-timeout=2000 

使用application.properties在您的應用程序下面的註釋添加到您的主類:

@PropertySources({ 
    @PropertySource({"classpath:application.properties"}) 
}) 

現在,用戶的實體可以是這樣的:

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.Id; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 

@Entity 
@Table(name = "USER") 
public class User { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "ID", nullable = false, updatable = false) 
    private Long id; 

    @Column(name = "LAST_NAME", nullable = false) 
    private String lastName; 

    @Column(name = "FIRST_NAME", nullable = false) 
    private String firstName; 

    @Column(name = "LOGGED_IN") 
    private boolean loggedIn; 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    public String getLastName() { 
     return lastName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

    public String getFirstName() { 
     return firstName; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    public boolean isLoggedIn() { 
     return loggedIn; 
    } 

    public void setLoggedIn(boolean loggedIn) { 
     this.loggedIn = loggedIn; 
    } 
} 

之後,你必須定義對Hibernate的存儲庫接口:

/** 
*Spring auto repository for database access of User objects. 
**/ 
public interface UserRepository extends JpaRepository<User, Long> { 

    Optional<User> findOneByName(String userName); 
} 

現在你可以做這樣的事情在你的登錄方法:

... 
vaadinSecurity.login(userName.getValue(), passwordField.getValue()); 
Optional<User> ouser = userRepository.findOneByName(userName); 
if (ouser.isPresent()) { 
    User user = ouser.get(); 
    user.setLoggedIn(true); 
} 
... 
+0

謝謝亞歷山大,但請問vaadinSecurity呢?它是什麼?謝謝 –

+0

'VaadinSecurity'來自vaadin4spring知識庫[link](https://github.com/peholmst/vaadin4spring)。這提供了一些不錯的服務,如I18N,安全和其他。看看[鏈接](https://vaadin.com/web/petter/home/-/blogs/experimenting-with-vaadin-spring-and-serialization?_33_redirect=https%3A%2F%2Fvaadin.com% 2Fweb%2Fpetter%2Fhome%3Fp_p_id%3D33%26p_p_lifecycle%3D0%26p_p_state%3Dnormal%26p_p_mode%3Dview%26p_p_col_id%3Dcolumn-1%26p_p_col_pos%3D2%26p_p_col_count%3D3)設計如何使用vaadin4spring。裏面還有一些樣品。 –

+0

我試圖用vaadin和spring啓動配置spring安全性,我使用SecurityConfig.java而不是xml文件我的問題現在我想創建我的自定義登錄視圖,但我不能,現在我仍然擁有默認的Spring登錄形式你有什麼想法? –