text="${name}">
th:
text="${name}"为thymeleaf语法,获取model中传过来的值
5.在浏览器访问http:
//localhost:
8080/testThymeleaf进行测试.如果能够在页面上获取到值就说明成功了.
此处需要注意在thymeleaf3.0以前对页面标签语法要求比较严格,开始标签必须有对应的结束标签,如果没有就出现如下错误.
如果页面标签不严谨还希望使用thymeleaf的话,那就需要升级thymeleaf到3.0以上的版本,此处升级为3.0.2
6.升级thymeleaf版本(修复上面的错误),在properties节点下面添加
--修改thymeleaf的版本-->
3.0.2.RELEASE
2.0.4
三、SpringBoot与Shiro整合实现用户认证
1.Shiro核心API类
Subject:
用户主体(把操作交给SecurityManager)
SecurityManager:
安全管理器(关联Realm)
Realm:
shiro连接数据库的桥梁
2.导入shiro与spring整合依赖
--shiro与Spring整合依赖-->
org.apache.shiro
shiro-spring
1.4.0
3.创建自定义Realm
packagecom.hellotomcat.shiro;
importorg.apache.shiro.authc.AuthenticationException;
importorg.apache.shiro.authc.AuthenticationInfo;
importorg.apache.shiro.authc.AuthenticationToken;
importorg.apache.shiro.authz.AuthorizationInfo;
importorg.apache.shiro.realm.AuthorizingRealm;
importorg.apache.shiro.subject.PrincipalCollection;
/***
*自定义Realm
*@authorLenovo
*
*/
publicclassUserRealmextendsAuthorizingRealm{
/***
*执行授权逻辑
*/
@Override
protectedAuthorizationInfodoGetAuthorizationInfo(PrincipalCollectionarg0){
System.out.println("执行授权逻辑");
returnnull;
}
/***
*执行认证逻辑
*/
@Override
protectedAuthenticationInfodoGetAuthenticationInfo(AuthenticationTokenarg0)throwsAuthenticationException{
System.out.println("执行认证逻辑");
returnnull;
}
}
4.编写shiro的配置类(重点)(最基础的配置类如下)
packagecom.hellotomcat.shiro;
importorg.apache.shiro.spring.web.ShiroFilterFactoryBean;
importorg.apache.shiro.web.mgt.DefaultWebSecurityManager;
importorg.springframework.beans.factory.annotation.Qualifier;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
/***
*Shiro的配置类
*@authorLenovo
*
*/
@Configuration
publicclassShiroConfig{
/***
*创建ShiroFilterFactoryBean
*/
publicShiroFilterFactoryBeangetShiroFilterFactoryBean(@Qualifier("securityManager")DefaultWebSecurityManagersecurityManager){
ShiroFilterFactoryBeanshiroFilterFactoryBean=newShiroFilterFactoryBean();
//设置安全管理器
shiroFilterFactoryBean.setSecurityManager(securityManager);
returnshiroFilterFactoryBean;
}
/***
*创建DefaultWebSecurityManager
*/
@Bean(name="securityManager")
publicDefaultWebSecurityManagergetDefaultWebSecurityManager(@Qualifier("userRealm")UserRealmuserRealm){
DefaultWebSecurityManagersecurityManager=newDefaultWebSecurityManager();
//关联realm
securityManager.setRealm(userRealm);
returnsecurityManager;
}
/***
*创建Realm
*/
@Bean
publicUserRealmgetRealm(){
returnnewUserRealm();
}
}
5.使用shiro内置过滤器实现拦截功能
1.
2.
3.
4.
5.
5.1.新建两个页面add.html和update.html
add.html页面代码:
DOCTYPEhtml>