shiro实现未登录拦截_Shiro 整合 Spring 实现登录 070

8e80c6df6cc07497616a22e620d4770b.png

自定义realm

package com.bjsxt.M_Realm;
import com.bjsxt.bean.user;
import com.bjsxt.service.Impl.UserServiceImpl;
import com.mysql.jdbc.Driver;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;/** * 认证 * */
public class Authentication_Realm extends AuthorizingRealm {
private String principal; //身份 用户名
private String credentials; //凭证 密码
private String salt; //盐值 加密
private String roleName; //角色名称
private String remark; //权限
private ResultSet rs;
private Statement state;
private java.sql.Connection conn;/** * 认证方法 获取认证信息 * AuthenticatingRealm * principal, 身份 用户名 * credentials, 凭证 密码 * salt 盐值 加密 * realmName * byteSource, * @param authenticationToken* @return* @throws AuthenticationException */
@Autowired
UserServiceImpl userService;
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//调用serivce 根据表单传过来的用户名查询用户信息
user user = userService.SelectName(token.getPrincipal().toString());
if(user!=null){
//获取盐值
ByteSource newsalt = ByteSource.Util.bytes(user.getPassword_salt());
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(token.getPrincipal(), user.getPassword(),newsalt,token.getPrincipal().toString());
System.out.println("info"+info);
return info;
}else {
return null;
}
}

web.xml 配置

<!--
注册shiro 核心过滤器
DelegatingFilterProxy:通过代理模式将servlet容器中的filter同Spring容器中的bean关联起来
-->
<filter>
<filter-name>delegatingFilterProxy</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<!--
targetFilterLifecycle 目标过滤器生命周期
属性为true 表明启用引入filter的init(初始化方法)和destroy(销毁方法)
表明 spring容器对应的filter生命周期 交给 servlet容器管理
-->
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
<!--
该属性设置Springr容器中filter的bean的id
-->
<init-param>
<param-name>targetBeanName</param-name>
<param-value>shiroFilter</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>delegatingFilterProxy</filter-name>
<url-pattern>/* </url-pattern>
</filter-mapping>

Spring-Shiro 配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--注册凭证匹配器-->
<bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<!--设置加密方式-->
<property name="hashAlgorithmName" value="MD5"/>
<!--设置迭代次数-->
<property name="hashIterations" value="1314"/>
</bean>
<!--注册 自定义realm-->
<bean id="authorization_realm" class="com.bjsxt.M_Realm.Authentication_Realm">
<!--引入凭证匹配器-->
<property name="credentialsMatcher" ref="credentialsMatcher"/>
</bean>
<!-- 注册 SecurityManager -->
<bean id="defaultWebSecurityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!--将自定义注入给 SecurityManager安全管理器-->
<property name="realm" ref="authorization_realm"/>
</bean>
<!--
配置shiro处理器
id必须与web.xm中的filter-name对应
-->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!--
引用 securityManager
-->
<property name="securityManager" ref="defaultWebSecurityManager"/>
<property name="loginUrl" value="/login"/>
<property name="successUrl" value="/jsp/success.jsp"/>
<property name="unauthorizedUrl" value="/jsp/unauthorized.jsp"/>
<!--
设置过滤器链
配置那些路径可以被访问或者被拦截
anon 过滤器的含义是匿名访问
authc 过滤器的含义是 必须认证之后才能访问 也就是必须登录之后才能访问
logout 过滤器的含义是退出
roles 角色过滤器
-->
<property name="filterChainDefinitions">
<value>
/login=authc
/**=anon
</value>
</property>
</bean>
</beans>

controller 实现

@RequestMapping("/login")
public String selectName(HttpServletRequest request, Model model){
//查看具体异常获取异常信息
Object ex = request.getAttribute(FormAuthenticationFilter.DEFAULT_ERROR_KEY_ATTRIBUTE_NAME);
System.out.println("ex"+ex);
if(UnknownAccountException.class.getName().equals(ex)){
System.out.println("UnknownAccountException"+UnknownAccountException.class.getName());
model.addAttribute("msg", "用户名不正确");
}else if(IncorrectCredentialsException.class.getName().equals(ex)){
System.out.println("IncorrectCredentialsException"+UnknownAccountException.class.getName());
model.addAttribute("msg", "凭证不正确");
}else{
model.addAttribute("msg", "未知异常");
}
return "jsp/exception";
}

login.jsp

<%@page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!</h2>
</body>
<h2>登录页面</h2>
<form action="/login" method="post">
用户名:<input type="text" name="username"/><br/>
密码:<input type="text" name="password"/><br/>
<input type="submit" value="登录">
</form>
</html>

usermapper.xml

<select id="select" resultType="user" parameterType="string">
select * from users where username=#{name}
</select>

实体类

package com.bjsxt.bean;
public class user {
private long id;
private String username;
private String password;
private String password_salt;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword_salt() {
return password_salt;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public void setPassword_salt(String password_salt) {
this.password_salt = password_salt;
}
public user(String username, String password) {
this.username = username;
this.password = password;
}
public user(String username, String password, String password_salt) {
this.username = username;
this.password = password;
this.password_salt = password_salt;
}
public user(long id, String username, String password, String password_salt) {
this.id = id;
this.username = username;
this.password = password;
this.password_salt = password_salt;
}
public user() {
}
@Override
public String toString() {
return "user{" +
"id=" + id +
", username='" + username + ''' +
", password='" + password + ''' +
", password_salt='" + password_salt + ''' +
'}';
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值