官术网_书友最值得收藏!

In-memory user storage

As mentioned earlier, for various testing purposes, it's better to store the user credentials and then authenticate in memory than to use a proper database, such as MySQL. For this, just change the Spring Security configuration file (SpringSecurityConfig.java) by adding the following method:

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("admin")
.password("{noop}admin@password")
//{noop} makes sure that the password encoder doesn't do anything
.roles("ADMIN") // Role of the user
.and()
.withUser("user")
.password("{noop}user@password")
.credentialsExpired(true)
.accountExpired(true)
.accountLocked(true)
.roles("USER");
}

It's important to note that the password has a prefix, {noop}, attached to it. This ensures that when the password is validated, no encoding is carried out. This is one way to avoid having password encoding errors when you run the project.

The full source code, as a fully fledged project, can be found on this book's GitHub page in the  jetty-in-memory-basic-authentication project.
主站蜘蛛池模板: 曲松县| 太湖县| 汕头市| 德惠市| 海南省| 合作市| 锡林郭勒盟| 禄丰县| 莆田市| 静乐县| 定陶县| 抚远县| 本溪市| 海阳市| 呼伦贝尔市| 长顺县| 兴宁市| 元谋县| 青岛市| 台湾省| 天全县| 孙吴县| 夏津县| 定兴县| 普格县| 双柏县| 五峰| 文山县| 晋江市| 北碚区| 嵊泗县| 东源县| 遂昌县| 云霄县| 惠安县| 新巴尔虎左旗| 博白县| 荆门市| 阳江市| 库车县| 社会|