如何在 Spring 引导中应用 URL 和基于角色的身份验证?
How to apply URL and role based authentication in Spring Boot?
我正在尝试通过以下方式应用 URl 和基于角色的身份验证
http
.authorizeRequests()
.antMatchers("/rest/**").hasRole("ADMIN")
.and()
.authorizeRequests()
.antMatchers("/admin/**").hasRole("MANAGER")
.and()
.authorizeRequests()
.antMatchers("/restApi/**").hasRole("USER")
.anyRequest().authenticated()
.and()
.formLogin()
.permitAll();
但是在输入用户名和密码后,我又回到了 Spring Boot 提供的默认登录屏幕。
如果我使用 permitAll()
而不是 hasRole()
,那么它可以正常工作。
我哪里错了?
dur 在这里回答了使用多个休息端点的好方法:
我用一个新项目测试了这个例子,必须在密码字符串前添加“{noop}”才能让它工作,但效果很好。
基本上我们将每个端点分离到其自己的 WebSecurityConfigurerAdapter 扩展中。
在这个例子中是:
http
.antMatcher("/api/**")
.authorizeRequests()
.anyRequest().hasRole("ADMIN")
.and().httpBasic();
虽然你将它颠倒过来并使用复数形式的 antmatchers 并且缺少 anyRequest()
(不确定这是否有所作为):
http
.authorizeRequests()
.antMatchers("/rest/**").hasRole("ADMIN")
我正在尝试通过以下方式应用 URl 和基于角色的身份验证
http
.authorizeRequests()
.antMatchers("/rest/**").hasRole("ADMIN")
.and()
.authorizeRequests()
.antMatchers("/admin/**").hasRole("MANAGER")
.and()
.authorizeRequests()
.antMatchers("/restApi/**").hasRole("USER")
.anyRequest().authenticated()
.and()
.formLogin()
.permitAll();
但是在输入用户名和密码后,我又回到了 Spring Boot 提供的默认登录屏幕。
如果我使用 permitAll()
而不是 hasRole()
,那么它可以正常工作。
我哪里错了?
dur 在这里回答了使用多个休息端点的好方法:
我用一个新项目测试了这个例子,必须在密码字符串前添加“{noop}”才能让它工作,但效果很好。
基本上我们将每个端点分离到其自己的 WebSecurityConfigurerAdapter 扩展中。
在这个例子中是:
http
.antMatcher("/api/**")
.authorizeRequests()
.anyRequest().hasRole("ADMIN")
.and().httpBasic();
虽然你将它颠倒过来并使用复数形式的 antmatchers 并且缺少 anyRequest() (不确定这是否有所作为):
http
.authorizeRequests()
.antMatchers("/rest/**").hasRole("ADMIN")