Spring Boot 整合 Shiro+Thymeleaf过程解析

网友投稿 243 2022-12-24


Spring Boot 整合 Shiro+Thymeleaf过程解析

这篇文章主要介绍了Spring Boot 整合 Shiro+Thymeleaf过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1.导包

org.apache.shiro

shiro-spring

1.4.1

com.github.theborakompanioni

thymeleaf-extras-shiro

2.0.0

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot

spring-boot-configuration-processor

true

org.projectlombok

lombok

true

2. 编写配置类

@Configuration

@ConfigurationProperties(prefix = "shiro")

@Data

public class ShiroConfig {

private String loginUrl;

private String unauthorizedUrl;

private String successUrl;

private String logoutUrl;

private String[] anons;

private String[] authcs;

/**

* 配置securityManager

* @param userRealm

* @return

*/

@Bean

public SecurityManager securityManager(UserRealm userRealm){

DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();

securityManager.setRealm(userRealm);

return securityManager;

}

/**

* 配置shiroFilter

* @param securityManager

* @return

*/

@Bean

public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager){

ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();

shiroFilteRJWjqbocFrFactoryBean.setSecurityManager(securityManager);

shiroFilterFactoryBean.setLoginUrl(loginUrl);

shiroFilterFactoryBean.setUnauthorizedUrl(unauthorizedUrl);

shiroFilterFactoryBean.setSuccessUrl(successUrl);

Map filterMap = new HashMap<>();

if(null != logoutUrl){

filterMap.put(loginUrl,"logout");

}

if(anons!=null && anons.length>0){

for(String anon:anons){

filterMap.put(anon,"anon");

}

}

if(authcs!=null && authcs.length>0){

for(String authc:authcs){

filterMap.put(authc,"authc");

}

}

shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);

return shiroFilterFactoryBean;

}

/**

* 配置自定义Realm

* @return

*/

@Bean

public UserRealm userRealm(CredentialsMatcher credentialsMatcher){

UserRealm userRealm = new UserRealm();

userRealm.setCredentialsMatcher(credentialsMatcher);

return userRealm;

}

/**

* 配置凭证匹配器

* @return

*/

@Bean

public HashedCredentialsMatcher hashedCredentialsMatcher(){

HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();

hashedCredentialsMatcher.setHashAlgorithmName("MD5");

hashedCredentialsMatcher.setHashIterations(10);

return hashedCredentialsMatcher;

}

/**

* 配置ShiroDialect,用于Thymeleaf和shiro标签的使用

* @return

*/

@Bean

public ShiroDialect shiroDialect(){

return new ShiroDialect();

}

}

3. application.yml 配置 拦截链

# shiro

shiro:

login-url: /login.html

anons:

- /login.html

- /index.html

- doLogin

authcs:

- /**


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java模拟客户端向服务器上传文件
下一篇:Java wait和notifyAll实现简单的阻塞队列
相关文章

 发表评论

暂时没有评论,来抢沙发吧~