![](/img/trans.png)
[英]how to achieve Ldap Authentication using spring security(spring boot)
[英]Authentication using Spring Boot using Mysql, Spring Data JPA and Spring Security
mysql> describe User;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| u_id | int | NO | PRI | NULL | auto_increment |
| email | varchar(30) | NO | UNI | NULL | |
| name | varchar(50) | NO | | NULL | |
| password | varchar(64) | NO | | NULL | |
| dob | varchar(30) | YES | | NULL | |
| role | varchar(10) | NO | | NULL | |
+----------+-------------+------+-----+---------+----------------+
这是我在 mysql 中的表。 他的角色列将只有 2 个值(管理员/用户)。 如果是用户,我需要授予对特定 url 的访问权限,并且同样适用于管理员。
如何为此使用 Spring 安全性和 JPA?
来自互联网的示例代码
http.authorizeRequests().antMatchers("/admin/**").hasRole("ADMIN").antMatchers("/users/**").hasRole("USER");
同样,我需要管理员访问 urls("/edit/"),("/add/") 和用户访问 urls("/rate/")
您需要创建一个扩展 WebSecurityConfigurerAdapter 并覆盖其方法的安全配置 class。
为了实现对 url 的基于角色的访问,使用了 antMatchers。 作为参考,我附加了一个在配置 class 中被覆盖的方法。
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/profile/**").hasAuthority("ROLE_USER")
.antMatchers("/dashboard/**").hasAuthority("ROLE_ADMIN")
.and()
.formLogin()
.loginPage("/login")
.usernameParameter("username")
.passwordParameter("password")
.successHandler(customSuccessHandler)
.permitAll()
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/login?logout")
.permitAll()
.and()
.exceptionHandling().accessDeniedPage("/403.html");
}
有关详细信息,请参阅https://www.baeldung.com/spring-security-expressions 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.