![](/img/trans.png)
[英]Struggling with adding spring security for angular js application
[英]Adding database to Spring Security and Angular JS
所以我跟着這個教程https://spring.io/guides/tutorials/spring-security-and-angular-js/
但是我無法弄清楚如何添加數據庫。
我已將此添加到屬性文件中,因此我可以連接到數據庫
spring.datasource.url=jdbc:mysql://localhost:3306/login
spring.datasource.username=root
spring.datasource.password=root
在 sql 我創建一個表如下
CREATE TABLE IF NOT EXISTS `login`.`users` (
`idusers` INT NOT NULL AUTO_INCREMENT COMMENT '',
`username` VARCHAR(45) NULL COMMENT '',
`password` VARCHAR(256) NULL COMMENT '',
`authority` VARCHAR(45) NULL COMMENT '',
PRIMARY KEY (`idusers`) COMMENT '')
ENGINE = InnoDB;
並添加了一些用戶。
我希望這用數據庫替換它。
@Autowired
public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user").password("password").roles("USER")
.and()
.withUser("admin").password("admin").roles("USER", "ADMIN", "READER", "WRITER")
.and()
.withUser("audit").password("audit").roles("USER", "ADMIN", "READER");
}
我感到困惑的是,在使用我自己的數據庫時,我仍然需要 Principal 用戶。
@RequestMapping("/user")
public Principal user(Principal user) {
System.out.println(user.getName());
System.out.println(user.toString());
return user;
}
查看文檔( http://docs.spring.io/spring-security/site/docs/4.0.2.RELEASE/reference/htmlsingle/#jc-authentication-jdbc ),看看你需要如何更改您的globalUserDetails
方法以使用jdbcAuthentication
而不是inMemoryAuthentication
:
@Autowired
private DataSource dataSource;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.jdbcAuthentication()
.dataSource(dataSource)
.withDefaultSchema()
.withUser("user").password("password").roles("USER").and()
.withUser("admin").password("password").roles("USER", "ADMIN");
}
實際配置適用於內存數據庫,因為它在初始化時創建一個新模式。 在您的情況下,您應該將其更改為:
@Autowired
private DataSource dataSource;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.jdbcAuthentication()
.dataSource(dataSource)
.usersByUsernameQuery(/* set a query to suit your model*/)
.authoritiesByUsernameQuery(/* set a query to suit your model*/)
.groupAuthoritiesByUsername(/* set a query to suit your model*/);
}
Principal
只是一個界面,讓您可以訪問當前登錄的用戶,不多不少。
更多關於 Spring MVC + 安全性的信息: http : //docs.spring.io/spring-security/site/docs/4.0.2.RELEASE/reference/htmlsingle/#mvc
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.