gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
spring security根据不同的角色访问不同的页面的代码是什么
- User
spring security根据不同的角色访问不同的页面的代码是什么
- Assistant
在Spring Security中,可以使用
antMatchers()方法来定义不同角色可以访问的页面。以下是一个示例代码,显示如何根据不同的角色访问不同的页面:@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/admin/**").hasRole("ADMIN") // 访问以/admin/开头的页面需要ADMIN角色 .antMatchers("/user/**").hasRole("USER") // 访问以/user/开头的页面需要USER角色 .anyRequest().authenticated() // 其他页面需要登录后访问 .and() .formLogin(); http.csrf().disable(); // 关闭CSRF防护 } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser("admin").password("admin").roles("ADMIN") // 创建一个具有ADMIN角色的用户 .and() .withUser("user").password("user").roles("USER"); // 创建一个具有USER角色的用户 } }在上面的示例中,我们使用
antMatchers()方法来定义不同角色可以访问的页面,例如以开头的页面需要ADMIN角色,以开头的页面需要USER角色。对于其他页面,用户需要进行身份验证后才能访问。通过方法启用默认的登录表单。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.