|
@@ -0,0 +1,76 @@
|
|
|
+package com.zhentao.shiro.config;
|
|
|
+
|
|
|
+import com.zhentao.shiro.config.MyRealm;
|
|
|
+import org.apache.shiro.authc.Authenticator;
|
|
|
+import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
|
|
|
+import org.apache.shiro.authz.Authorizer;
|
|
|
+import org.apache.shiro.authz.ModularRealmAuthorizer;
|
|
|
+import org.apache.shiro.mgt.SecurityManager;
|
|
|
+import org.apache.shiro.session.mgt.SessionManager;
|
|
|
+import org.apache.shiro.session.mgt.eis.MemorySessionDAO;
|
|
|
+import org.apache.shiro.session.mgt.eis.SessionDAO;
|
|
|
+import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
|
|
|
+import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
|
|
|
+import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
|
|
|
+import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
|
|
|
+import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+
|
|
|
+import java.util.Collections;
|
|
|
+
|
|
|
+@Configuration
|
|
|
+public class ShiroConfig {
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager mySecurityManager) {
|
|
|
+ ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
|
|
|
+ shiroFilterFactoryBean.setSecurityManager(mySecurityManager);
|
|
|
+ // 登录页面
|
|
|
+ shiroFilterFactoryBean.setLoginUrl("/login");
|
|
|
+ // 未授权页面
|
|
|
+ shiroFilterFactoryBean.setUnauthorizedUrl("/unauthorized");
|
|
|
+ return shiroFilterFactoryBean;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public SecurityManager mySecurityManager(Authenticator authenticator, Authorizer authorizer, SessionManager sessionManager) {
|
|
|
+ DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
|
|
|
+ securityManager.setAuthenticator(authenticator); // 使用配置好的 Authenticator
|
|
|
+ securityManager.setAuthorizer(authorizer);
|
|
|
+ securityManager.setSessionManager(sessionManager);
|
|
|
+ return securityManager;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public ShiroFilterChainDefinition shiroFilterChainDefinition() {
|
|
|
+ DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
|
|
|
+ // 配置哪些请求需要受保护,以及访问这些请求需要的权限
|
|
|
+ chainDefinition.addPathDefinition("/login", "anon"); // 登录接口可匿名访问
|
|
|
+ chainDefinition.addPathDefinition("/unauthorized", "anon"); // 未授权页面可匿名访问
|
|
|
+ chainDefinition.addPathDefinition("/**", "authc"); // 其他请求需要认证
|
|
|
+ return chainDefinition;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public Authenticator authenticator(MyRealm myRealm) { // 注入 MyRealm
|
|
|
+ ModularRealmAuthenticator authenticator = new ModularRealmAuthenticator();
|
|
|
+ // 将 MyRealm 添加到 Authenticator 的 realms 列表中
|
|
|
+ authenticator.setRealms(Collections.singletonList(myRealm));
|
|
|
+ return authenticator;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public Authorizer authorizer() {
|
|
|
+ return new ModularRealmAuthorizer();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public SessionManager sessionManager() {
|
|
|
+ DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
|
|
|
+ // 这里可以进行更多的 sessionManager 配置,例如设置 sessionDAO
|
|
|
+ SessionDAO sessionDAO = new MemorySessionDAO();
|
|
|
+ sessionManager.setSessionDAO(sessionDAO);
|
|
|
+ return sessionManager;
|
|
|
+ }
|
|
|
+}
|