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

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.
主站蜘蛛池模板: 施秉县| 鲁甸县| 凤冈县| 民丰县| 容城县| 临武县| 隆尧县| 黎川县| 隆昌县| 犍为县| 长兴县| 宣武区| 府谷县| 韶山市| 都江堰市| 屏东县| 府谷县| 偏关县| 柳林县| 上犹县| 施秉县| 马关县| 古交市| 达拉特旗| 湖口县| 辽宁省| 西充县| 和平县| 苍南县| 龙泉市| 忻城县| 崇明县| 宝应县| 隆昌县| 凉城县| 资阳市| 长宁区| 文昌市| 怀安县| 龙山县| 新巴尔虎右旗|