com.baomidou
mybatisplus-spring-boot-starter
${mybatisplus.spring.boot.version}
org.springframework.boot
spring-boot-devtools
true
自动重启spring boot 也挺耗时间的,在App类main方法中加上下面红色这句,开发时如果有小的改动(不新增方法、变量)时,可以不自动重启而代码生效,System.setProperty("spring.devtools.restart.enabled", "false");。
public static void main(String[] args) {
System.setProperty("spring.devtools.restart.enabled", "false");
SpringApplication.run(DataApplication.class, args);
}
直接上代码,使用方法1、手工切换2、AOP实现
public class DynamicDataSource extends AbstractRoutingDataSource {
private static final ThreadLocal contextHolder = new ThreadLocal<>();
public DynamicDataSource(DataSource defaultTargetDataSource, Map
手工切换方式如下
DynamicDataSource.setDataSource("second");
AcctUserEntity entity = acctUserService.selectById(11);
DynamicDataSource.clearDataSource();
AOP方法切换
<#assign ctx=request.getContextPath()>
然后在js或者页面里就可以直接这样用了
public class MyFormAuthenticationFilter extends FormAuthenticationFilter {
@Override
public boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request,
ServletResponse response) throws IOException {
// 无论认证前的url是什么,认证成功后跳到index
WebUtils.issueRedirect(request, response, getSuccessUrl());
return false;
}
}
WebUtils.issueRedirect(request, response, getSuccessUrl());
return false;
}
}
在jsp中使用shiro标签比较简单,在freemarker中使用要加入maven依赖
net.mingsoft
shiro-freemarker-tags
0.1
配置类FreemarkerConfig来对Freemark使用Shiro标签进行配置,引入
@Configuration
public class FreemarkerConfig {
@Bean
public FreeMarkerConfigurer freeMarkerConfigurer() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPath("classpath:/templates");
Map variables = new HashMap<>(1);
variables.put("shiro", new ShiroTags());
configurer.setFreemarkerVariables(variables);
Properties settings = new Properties();
settings.setProperty("default_encoding", "utf-8");
settings.setProperty("number_format", "0.##");
configurer.setFreemarkerSettings(settings);
return configurer;
}
}
我们看下其中的ShiroTags类,查看每个tag的定义,我们也可以自定义自己的标签
package com.jagregory.shiro.freemarker;
import freemarker.template.SimpleHash;
/**
* Shortcut for injecting the tags into Freemarker
*
* Usage: cfg.setSharedVeriable("shiro", new ShiroTags());
*/
public class ShiroTags extends SimpleHash {
public ShiroTags() {
put("authenticated", new AuthenticatedTag());
put("guest", new GuestTag());
put("hasAnyRoles", new HasAnyRolesTag());
put("hasPermission", new HasPermissionTag());
put("hasRole", new HasRoleTag());
put("lacksPermission", new LacksPermissionTag());
put("lacksRole", new LacksRoleTag());
put("notAuthenticated", new NotAuthenticatedTag());
put("principal", new PrincipalTag());
put("user", new UserTag());
}
}
在freemarker页面中使用标签
<@shiro.hasPermission name="P_D_AUDIT">
@shiro.hasPermission>
<@shiro.principal property="userName" />
<@shiro.guest>
游客访问
@shiro.guest>