spring boot 集成shiro 登录二次
最近又把这个shiro用到项目中,但是执行/login的,是这样的流程:
1、先执行了 protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException
2、到 /login (Controller层里的方法) 里面去执行
这样的话,就Creating a new SqlSession二次,一次是null(还没有进方法,当然是null)
另一次是正常的。
3、那发生这个情况是什么引起的。
1) 猜想是jar包版本引起的,但后来感觉不可能。
2)是不是可以加一个判断,然后是null就不走了,其实还是到doGetAuthenticationInfo里面去的,只是不走查询数据的操作(不合理)。
3)如果是这样的话,那可能是因为本身/login这个方法就需要去拦截,所以先走doGetAuthenticationInfo了,再进入方法去执行。
就3)了,解决:
@Bean public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) { ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); shiroFilterFactoryBean.setSecurityManager(securityManager); Map<String, String> map = new HashMap<>(); //authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问 //登出 map.put("/logout", "logout"); //对所有用户认证 map.put("/**", "authc"); map.put("/user/login", "anon"); //登录 shiroFilterFactoryBean.setLoginUrl("/user/login"); //首页 shiroFilterFactoryBean.setSuccessUrl("/user/index"); //错误页面,认证不通过跳转 shiroFilterFactoryBean.setUnauthorizedUrl("/user/error"); shiroFilterFactoryBean.setFilterChainDefinitionMap(map); return shiroFilterFactoryBean; }
标记上"anon",这样就不用去验证了,当然不会去执行。只有调用Subject.login方法才会去走。
如果上面的password不赋值,也会执行二次。
集成shiro:
https://blog.csdn.net/bicheng4769/article/details/86668209
配制:
https://blog.csdn.net/njpkhuan/article/details/100563123
参考:
https://blog.csdn.net/weixin_39973810/article/details/85113351
道法自然