常用配置
https://www.cnblogs.com/zimug/p/11870861.html
@Configurationpublic class SecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.csrf().disable() //禁用跨站csrf攻击防御,后面的章节会专门讲解.formLogin().loginPage("/login.html")//用户未登录时,访问任何资源都转跳到该路径,即登录页面.loginProcessingUrl("/login")//登录表单form中action的地址,也就是处理认证请求的路径.usernameParameter("uname")///登录表单form中用户名输入框input的name名,不修改的话默认是username.passwordParameter("pword")//form中密码输入框input的name名,不修改的话默认是password.defaultSuccessUrl("/index")//登录认证成功后默认转跳的路径.and().authorizeRequests().antMatchers("/login.html","/login").permitAll()//不需要通过登录验证就可以被访问的资源路径.antMatchers("/biz1").hasAnyAuthority("biz1") //前面是资源的访问路径、后面是资源的名称或者叫资源ID.antMatchers("/biz2").hasAnyAuthority("biz2").antMatchers("/syslog").hasAnyAuthority("syslog").antMatchers("/sysuser").hasAnyAuthority("sysuser").anyRequest().authenticated();}}
@Overridepublic void configure(WebSecurity web) {//将项目中静态资源路径开放出来web.ignoring().antMatchers("/config/**", "/css/**", "/fonts/**", "/img/**", "/js/**");}
hasrole和hasAnyAuthority的区别
hasRole:
角色授权:授权代码,在我们返回的UserDetails的Authority需要加ROLE前缀,Controller上使用时不要加前缀;
hasAuthority:
权限授权:用户自定义的权限,返回的UserDetails的Authority只要与这里匹配就可以,这里不需要加ROLE,名称保持一至即可
https://blog.csdn.net/qq_26878363/article/details/103632459
sendRedirect和out.close顺序问题
//正确resp.setContentType("application/json;charset=utf-8");PrintWriter out=resp.getWriter();out.write(JSONObject.toJSONString(ResultInfo.success("登出成功!",200)));out.flush();out.close();//错误resp.setContentType("application/json;charset=utf-8");PrintWriter out=resp.getWriter();out.write(JSONObject.toJSONString(ResultInfo.success("登出成功!",200)));resp.sendRedirect("/index.html");out.flush();out.close();resp.sendRedirect("/index.html");
https://blog.csdn.net/a3226988/article/details/80957932
配置角色继承关系记得加空格和ROLE前缀
@BeanRoleHierarchy roleHierarchy(){RoleHierarchyImpl roleHierarchy=new RoleHierarchyImpl();//不加空格无效String hierarchy="ROLE_dba > ROLE_admin > ROLE_user";roleHierarchy.setHierarchy(hierarchy);return roleHierarchy;}
