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

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.
主站蜘蛛池模板: 伊川县| 中西区| 古浪县| 衡山县| 衡山县| 正安县| 航空| 延津县| 宁陕县| 玛沁县| 彝良县| 呼图壁县| 河曲县| 肥西县| 象山县| 曲周县| 隆林| 比如县| 无极县| 洛隆县| 景德镇市| 林州市| 奎屯市| 来凤县| 河津市| 泸西县| 汉川市| 侯马市| 安丘市| 乌恰县| 壶关县| 富源县| 巍山| 宁武县| 甘南县| 获嘉县| 大邑县| 曲沃县| 托克托县| 德化县| 霞浦县|