布吉网站建设哪家好,wordpress快速下载地址,东莞高端品牌网站建设价格,asp网站源码使用基于角色或权限进行访问控制
hasAuthority 方法
如果当前的主体具有指定的权限#xff0c;则返回 true,否则返回 false
在配置类设置当前访问地址有哪些 Overrideprotected void configure(HttpSecurity http) throws Exception {http.formLogin() //自定义自己编写的登…基于角色或权限进行访问控制
hasAuthority 方法
如果当前的主体具有指定的权限则返回 true,否则返回 false
在配置类设置当前访问地址有哪些 Overrideprotected void configure(HttpSecurity http) throws Exception {http.formLogin() //自定义自己编写的登陆页面.loginPage(/login.html) //登陆页面设置.loginProcessingUrl(/user/login) //登陆访问路径.defaultSuccessUrl(/test/index).permitAll() //登陆成功之后跳转路径.and().authorizeRequests().antMatchers(/,/test/hello,/user/login).permitAll() //设置哪些路径可以直接访问不需要认证//当前登陆用户只有具有admin权限才可以访问这个路径.antMatchers(/test/index).hasAuthority(admins).anyRequest().authenticated().and().csrf().disable(); //关闭csrf防护}在UserDetailsService把返回User对象设置权限 Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {//调用userMapper方法根据用户名查询数据库QueryWrapperUsers wrapper new QueryWrapper();//where username ?wrapper.eq(username,username);Users users usersMapper.selectOne(wrapper);//判断if (usersnull)//数据库没有用户名认证失败{throw new UsernameNotFoundException(用户名不存在);}ListGrantedAuthority auths AuthorityUtils.commaSeparatedStringToAuthorityList(admins);return new User(users.getUsername(),new BCryptPasswordEncoder().encode(users.getPassword()),auths);}测试 没有访问权限 403 hasAnyAuthority 方法
如果当前的主体有任何提供的“角色”的话返回true. Overrideprotected void configure(HttpSecurity http) throws Exception {http.formLogin() //自定义自己编写的登陆页面.loginPage(/login.html) //登陆页面设置.loginProcessingUrl(/user/login) //登陆访问路径.defaultSuccessUrl(/test/index).permitAll() //登陆成功之后跳转路径.and().authorizeRequests().antMatchers(/,/test/hello,/user/login).permitAll() //设置哪些路径可以直接访问不需要认证
// //当前登陆用户只有具有admin权限才可以访问这个路径
// .antMatchers(/test/index).hasAuthority(admins).antMatchers(/test/index).hasAnyAuthority(admins”,“manager).anyRequest().authenticated().and().csrf().disable(); //关闭csrf防护}hasRole 方法
如果用户具备给定角色就允许访问,否则出现 403。 如果当前主体具有指定的角色则返回 true。 从源码中我们可以看出前面会自动加个ROLE_ hasAnyRole
表示用户具备任何一个条件都可以访问。