shiro 在spring框架中集成shiro

spring版本5.0.12
shiro版本 1.3.2

1、web.xml

 
    <filter>
        <filter-name>shiroFilterfilter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxyfilter-class>
        <init-param>
            <param-name>targetFilterLifecycleparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>

    <filter-mapping>
        <filter-name>shiroFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

2、集成Shiro

在Spring中集成Shiro时,Shiro中的相应Bean的定义以及他们的关系也需要通过Spring IoC容器实现,配置如下

 <bean class="com.tf.myshiro.web.service.CustomRealm" id="customRealm">
        <property name="credentialsMatcher" ref="credentialsMatcher" />
    bean>

    <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher" id="credentialsMatcher">
        <property name="hashAlgorithmName" value="md5" />
        <property name="hashIterations" value="1" />
    bean>
    
    <bean class="org.apache.shiro.web.mgt.DefaultWebSecurityManager" id="securityManager">
        <property name="realm" ref="customRealm" />
    bean>

    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">

        <property name="securityManager" ref="securityManager" />
        <property name="loginUrl" value="login.html" />
        <property name="unauthorizedUrl" value="403.html" />
        <property name="filterChainDefinitions">
            <value>
                
                /login.html = anon
                /subLogin.do= anon
                /login.jsp = anon
                
                
                
                
                
                
                /* = authc
            value>
        property>
    bean>

3、UserController

@Controller
public class UserController {
    @RequestMapping(value = "/subLogin", method = RequestMethod.POST)//,produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String subLogin(User user) {

        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(),
                user.getPassword());

        try {
            if (user.getRememberMe() != null)
                token.setRememberMe(user.getRememberMe());
            subject.login(token);
        } catch (AuthenticationException e) {
            return e.getMessage();
        }

        if (subject.hasRole("admin")) {
            return "有admin权限";
        }

//        return "登录成功";
        return "无admin权限";
    }
}

4、html


<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Logintitle>
head>
<body>
<form action="subLogin.do" method="post">
    <div>
        <label>用户名:label>
        <input type="text" name="username">
    div>
    <div>
        <label>密码:label>
        <input type="password" name="password">
    div>
    <div>
        <label>记住我:label>
        <input type="checkbox" name="rememberMe">
    div>
    <div>
        <input type="submit" value="登录">
    div>
form>
body>
html>

你可能感兴趣的:(Java)