@TOC关于 springboot thymeleaf 整合shiro 标签失效不进入doGetAuthorizationInfo问题
之前是使用注解 @RequiresPermissions()在每个方法加上,就可以进入doGetAuthorizationInfo授权方法,后面改成前端只使用shiro标签触发调用doGetAuthorizationInfo方法,不知道为什么死活不行,百度了四天,用了各种方法都不行,今天终于解决了,
1.先导入依赖
<dependency>
<groupId>com.github.theborakompanioni</groupId>
<artifactId>thymeleaf-extras-shiro</artifactId>
<version>2.0.0</version>
</dependency>
2.引用bean
@Bean
public ShiroDialect shiroDialect() {
return new ShiroDialect();
}
3.重点,我就是死在这里,前面2个百度上一搜大把人都说加这两个就可以了,我反正加了就不行,后面加了下面这个就可以了。
因为springboot支持使用 thymeleaf模板引擎,所以这个也要引入
**
下面是我的 thymeleaf配置类,仅供参考,不可全抄,看重点,把上面图划线的地方拿走就可以了,不然项目运行不起来不要怪我
**
import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.HttpPutFormContentFilter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.dialect.IDialect;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import javax.servlet.MultipartConfigElement;
import java.util.HashSet;
import java.util.Set;
/**
-
thymeleaf配置类
*/
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}/**
- 模板资源解析器
- @return
*/
@Bean
@ConfigurationProperties(prefix = “spring.thymeleaf”)
public SpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setApplicationContext(this.applicationContext);
templateResolver.setCharacterEncoding(“UTF-8”);
templateResolver.setCacheable(false);
return templateResolver;
}
/**
-
Thymeleaf标准方言解释器
*/
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
// 支持Spring EL表达式
templateEngine.setEnableSpringELCompiler(true);Set additionalDialects = new HashSet();
additionalDialects.add(new ShiroDialect());
templateEngine.setAdditionalDialects(additionalDialects);
return templateEngine;
}
/**
- 视图解析器
*/
@Bean
public ThymeleafViewResolver viewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
return viewResolver;
}
@Bean
public HttpPutFormContentFilter httpPutFormContentFilter() {
return new HttpPutFormContentFilter();
}
}
到此大功告成