项目用到了ssm+layui+shiro.
今天在写项目中碰到了一个让人无语的问题,在登录页面实现验证码时,总是出现302状态码,最后解决发现验证码在前端页面不显示的原因是需要授权,所以我就在shiro中放过了这个验证码的路径就成功解决了这个问题。
验证码路径: /user/yan
在shrio的xml文件中的shiro过滤链配置中,配置这个验证码的路径允许匿名访问就完美的解决了这个问题。
这是shiro的xml中的工厂:
<!--过滤器的工厂bean-->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="loginUrl" value="/user/login"/>
<property name="successUrl" value="/user/success"/>
<property name="securityManager" ref="securityManager"/>
<!--自定义filter-->
<property name="filters">
<map>
<!--将自定义的formAuthenticationFilter注入到shiroFilter中-->
<entry key="authc" value-ref="formAuthenticationFilter"></entry>
</map>
</property>
<!--shiro过滤器链配置-->
<property name="filterChainDefinitions">
<value>
<!--配置不登录可以进行访问的路径(允许匿名访问的资源)-->
/layui/**=anon
/css/**=anon
/js/**=anon
/api/**=anon
/images/**=anon
/lib/**=anon
/user/yan=anon
<!--配置所有的资源必须认证通过才可访问-->
/user/logout=logout
/**=authc
</value>
</property>
</bean>
/**
* 获取验证码图片
* @return
*/
@RequestMapping("/yan")
public void getCode(HttpServletResponse response, HttpSession session) {
//HuTool定义图形验证码的长和宽,验证码的位数,干扰线的条数
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(120, 40,4,10);
//将验证码放入session
session.setAttribute("code",lineCaptcha.getCode());
try {
ServletOutputStream outputStream = response.getOutputStream();
lineCaptcha.write(outputStream);
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}