言尽于此,完结
无论是一个初级的 coder,高级的程序员,还是顶级的系统架构师,应该都有深刻的领会到设计模式的重要性。
- 第一,设计模式能让专业人之间交流方便,如下:
程序员A:这里我用了XXX设计模式
程序员B:那我大致了解你程序的设计思路了
- 第二,易维护
项目经理:今天客户有这样一个需求…
程序员:明白了,这里我使用了XXX设计模式,所以改起来很快
- 第三,设计模式是编程经验的总结
程序员A:B,你怎么想到要这样去构建你的代码
程序员B:在我学习了XXX设计模式之后,好像自然而然就感觉这样写能避免一些问题
- 第四,学习设计模式并不是必须的
程序员A:B,你这段代码使用的是XXX设计模式对吗?
程序员B:不好意思,我没有学习过设计模式,但是我的经验告诉我是这样写的
从设计思想解读开源框架,一步一步到Spring、Spring5、SpringMVC、MyBatis等源码解读,我都已收集整理全套,篇幅有限,这块只是详细的解说了23种设计模式,整理的文件如下图一览无余!
搜集费时费力,能看到此处的都是真爱!
<context:component-scan base-package=“cn.shiro” />
<mvc:annotation-driven />
<bean name=“springContextUtil” class=“cn.shiro.utils.SpringContextUtil”
scope=“singleton”>
<aop:config proxy-target-class=“true” />
<bean id=“viewResolver”
class=“org.springframework.web.servlet.view.InternalResourceViewResolver”
p:prefix=“/WEB-INF/views/” p:suffix=“.jsp” />
注:<aop:config proxy-target-class="true" />
没有的话可能会出现身份验证的时候验证角色不验证权限。
spring_mybaits.xml:(通过mybatis连接数据库,不需要改动,直接拉过来)
spring_shiro.xml:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:p=“http://www.springframework.org/schema/p” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xmlns:context=“http://www.springframework.org/schema/context”
xmlns:mvc=“http://www.springframework.org/schema/mvc”
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
/one/**=authc
/login.jsp=authc
/logout=logout
/**=authc
<bean class=“org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator”
depends-on=“lifecycleBeanPostProcessor” />
<bean
class=“org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor”>
SampleRealm.java:
package cn.shiro.realm;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import cn.shiro.pojo.UUser;
public class SampleRealm extends AuthorizingRealm {
/**
- 认证信息,主要针对用户登录,
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken authcToken) throws AuthenticationException {
String username = (String) authcToken.getPrincipal();
String password = new String((char[]) authcToken.getCredentials());
UUser user = new UUser();
user.setEmail(username);
user.setId(1L);
user.setPswd(password);
user.setStatus(1L);
System.err.println(user);
if(UUser._0.equals(user.getStatus())){
throw new DisabledAccountException(“帐号已经禁止登录!”);
}else{
//更新登录时间 last login time
user.setLastLoginTime(new Date());
}
return new SimpleAuthenticationInfo(user,user.getPswd(), getName());
}
/**
- 授权
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
UUser user = (UUser)principals.getPrimaryPrincipal();
//根据用户ID查询角色(role),放入到Authorization里。
Set roles = new HashSet();
roles.add(user.getEmail());
info.setRoles(roles);
System.out.println(roles);
//根据用户ID查询权限(permission),放入到Authorization里。
Set permissions = new HashSet();
permissions.add(“admin:insert”);
permissions.add(“admin:del”);
System.out.println(permissions.toString());
info.setStringPermissions(permissions);
return info;
}
/**
- 清空当前用户权限信息
*/
public void clearCachedAuthorizationInfo() {
PrincipalCollection principalCollection = SecurityUtils.getSubject().getPrincipals();
SimplePrincipalCollection principals = new SimplePrincipalCollection(
principalCollection, getName());
super.clearCachedAuthorizationInfo(principals);
}
/**
- 指定principalCollection 清除
*/
public void clearCachedAuthorizationInfo(PrincipalCollection principalCollection) {
SimplePrincipalCollection principals = new SimplePrincipalCollection(
principalCollection, getName());
super.clearCachedAuthorizationInfo(principals);
}
}
注:AuthorizingRealm是必须继承的。实现里面的doGetAuthenticationInfo(验证角色)doGetAuthorizationInfo(查询权限)方法。没有连接数据库,查询数据库验证权限这一步我直接使用死数据来模拟。之前研究角色表关系走了很大的弯路,透过现象看本质,本质上就是查询出角色写入到对象中去。
controller.java
package cn.shiro.controller;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(“/one/”)
public class OneController {
@RequiresAuthentication // 要求用户必须要登录才可以访问这个资源
@RequiresRoles(“admin”) // 要求用户必须是admin角色才可以访问这个方法
@RequiresPermissions(“admin:del”) // 要求的权限
@RequestMapping(“show”)
@ResponseBody
public String show() {
System.err.println(“Show…” + this);
return “b”;
}
}
UUser.java
package cn.shiro.pojo;
import java.io.Serializable;
import java.util.Date;
import net.sf.json.JSONObject;
public class UUser implements Serializable {
private static final long serialVersionUID = 1L;
// 0:禁止登录
public static final Long _0 = new Long(0);
// 1:有效
public static final Long _1 = new Long(1);
private Long id;
/** 昵称 */
private String nickname;
/** 邮箱 | 登录帐号 */
private String email;
/** 密码 */
private transient String pswd;
/** 创建时间 */
private Date createTime;
/** 最后登录时间 */
private Date lastLoginTime;
/** 1:有效,0:禁止登录 */
private Long status;
public UUser() {
}
public UUser(UUser user) {
this.id = user.getId();
this.nickname = user.getNickname();
this.email = user.getEmail();
this.pswd = user.getPswd();
this.createTime = user.getCreateTime();
this.lastLoginTime = user.getLastLoginTime();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getEmail() {
return email;
}
public Long getStatus() {
return status;
}
public void setStatus(Long status) {
this.status = status;
}
总结
在这里,由于面试中MySQL问的比较多,因此也就在此以MySQL为例为大家总结分享。但是你要学习的往往不止这一点,还有一些主流框架的使用,Spring源码的学习,Mybatis源码的学习等等都是需要掌握的,我也把这些知识点都整理起来了
;
}
public String getEmail() {
return email;
}
public Long getStatus() {
return status;
}
public void setStatus(Long status) {
this.status = status;
}
总结
在这里,由于面试中MySQL问的比较多,因此也就在此以MySQL为例为大家总结分享。但是你要学习的往往不止这一点,还有一些主流框架的使用,Spring源码的学习,Mybatis源码的学习等等都是需要掌握的,我也把这些知识点都整理起来了
[外链图片转存中…(img-XfLE4m8j-1714869675887)]
[外链图片转存中…(img-jwaEt9Rx-1714869675888)]