springboot整合shiro[springboot10]

springboot整合shiro

(项目已实现“登录拦截”和“认证”)

项目结构:

springboot整合shiro[springboot10]_第1张图片

所用依赖:

pom.xml


        
<dependency>
    <groupId>org.apache.shirogroupId>
    <artifactId>shiro-springartifactId>
    <version>1.4.1version>
dependency>



<dependency>
   <groupId>org.thymeleafgroupId>
   <artifactId>thymeleaf-spring5artifactId>
dependency>

<dependency>
  <groupId>org.thymeleaf.extrasgroupId>
  <artifactId>thymeleaf-extras-java8timeartifactId>
dependency>

ShiroConfig.java

@Configuration
public class ShiroConfig {
     

//    ShiroFilterFactoryBean:3
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
     
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
//        设置安全管理器
        bean.setSecurityManager(defaultWebSecurityManager);
//        添加shiro的内置过滤器
        /**
         * anon:无需认证就可以访问
         * authc:必须认证了才能访问
         * user:必须拥有 记住我 功能才能用
         * perms: 拥有对某个资源的权限才能访问
         * role :拥有某个角色权限才能访问
         */
//        拦截
        Map<String, String> filterMap = new LinkedHashMap<>();
//        可以用代码替换 filterMap.put("/user/*","authc");
        filterMap.put("/user/add","anon");
        filterMap.put("/user/update","authc");

        bean.setFilterChainDefinitionMap(filterMap);

//        设置登录的请求
        bean.setLoginUrl("/toLogin");

        return bean;
    }


//    DefaultWebSecurityManager:2
    @Bean(name="securityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
     
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
//        关联UserRealm
        securityManager.setRealm(userRealm);
        return securityManager;
    }

//    创建realm对象,需要自定义类:1
    @Bean
    public UserRealm userRealm(){
     
        return new UserRealm();
    }

}

UserReaml.java

//自定义的UserRealm extends AuthorizingRealm
public class UserRealm extends AuthorizingRealm {
     
//    授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
     
        System.out.println("执行了->授权doGetAuthorizationInfo");
        return null;
    }

//    认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
     
        System.out.println("执行了->认证doGetAuthorizationInfo");
//        用户名,密码,数据中取
        String name = "root";
        String password = "123456";
        UsernamePasswordToken userToken = (UsernamePasswordToken) authenticationToken;
        if(!userToken.getUsername().equals(name)){
     
            return null;//抛出异常 UnkownAccountException
        }
        //密码认证,shiro做
        return new SimpleAuthenticationInfo("",password,"");
    }
}

MyController.java

@Controller
public class MyController {
     

    @RequestMapping({
     "/","/index"})
    public String toIndex(Model model){
     
            model.addAttribute("msg","hello,shiro");
            return "index";
    }

    @RequestMapping("/user/add")
    public String add(){
     
        return "user/add";
    }

    @RequestMapping("/user/update")
    public String update(){
     
        return "user/update";
    }

    @RequestMapping("/toLogin")
    public String toLogin(){
     
        return "login";
    }

    @RequestMapping("/login")
    public String login(String username,String password,Model model){
     
//        获取当前的用户
        Subject subject = SecurityUtils.getSubject();
//        封装用户的登录数据
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try{
     
//        执行登录方法,如果没有异常就说明ok了
            subject.login(token);
            return "index";
        }catch (UnknownAccountException e){
       //用户名不存在
            model.addAttribute("msg","用户名错误");
            return "login";
        }catch (IncorrectCredentialsException e){
     //密码不存在
            model.addAttribute("msg","密码错误");
            return "login";
        }
    }


}

index.html


<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<body>
<h1>首页h1>
<div th:text="${msg}">div>
<hr>

<a th:href="@{/user/add}">adda> | <a th:href="@{/user/update}">updatea>

body>
html>

login.html


<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<body>
<h1>登录h1>
<hr>
<p th:text="${msg}" style="color: red;">p>
<form th:action="@{/login}">
    <p>用户名:<input type="text" name="username"> p>
    <p>密码:<input type="text" name="password"> p>
    <p><input type="submit">p>
form>
body>
html>

add.html


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<body>
<h1>addh1>
body>
html>

update.html


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<body>
<h1>updateh1>
body>
html>

你可能感兴趣的:(spring,shiro)