一、專案簡述
功能:一個基于JavaWeb的網上書店的設計與實作,歸納 出了幾個模塊,首先是登錄注冊模塊,圖書查找模塊,購 物車模塊,訂單模塊,個人中心模塊,用戶管理模塊,圖 書管理模塊等, 該專案是javaJeb技術的實戰操作,采用了MVC設計模 式,包括基本的entity, jscript, servlet,以及ajax異步請 求,查詢分頁,持久化層方法的封裝等等,對javaweb技 術的鞏固很有幫助,為J2EE的學習打下基礎,適用于課程 設計,畢業設計,
二、專案運行
環境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe (IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持),
專案技術: JSP + Entity+ Servlert + html+ css + JavaScript + JQuery + Ajax + Fileupload 等等,




書資訊管理代碼:
書資訊管理:
@Controller
@RequestMapping("/book")
public class BookInfoController {
@Autowired
private IBookInfoService bookInfoService;
@Autowired
private BookDescMapper bookDescMapper;
/**
* 查詢某一本書籍詳情
*
* @param bookId
* @param model
* @return
*/
@RequestMapping("/info/{bookId}")
public String bookInfo(@PathVariable("bookId") Integer bookId, Model model) throws BSException {
//查詢書籍
BookInfo bookInfo = bookInfoService.findById(bookId);
//查詢書籍推薦串列
List<BookInfo> recommendBookList = bookInfoService.findBookListByCateId(bookInfo.getBookCategoryId(), 1, 5);
//查詢書籍詳情
BookDesc bookDesc = bookDescMapper.selectByPrimaryKey(bookId);
//增加訪問量
bookInfoService.addLookMount(bookInfo);
Collections.shuffle(recommendBookList);
model.addAttribute("bookInfo", bookInfo);
model.addAttribute("bookDesc", bookDesc);
model.addAttribute("recommendBookList", recommendBookList);
return "book_info";
}
/**
* 通過關鍵字和書籍分類搜索書籍串列
*
* @param keywords
* @return
*/
@RequestMapping("/list")
public String bookSearchList(@RequestParam(defaultValue = "", required = false) String keywords,
@RequestParam(defaultValue = "0", required = false) int cateId,//分類Id,默認為0,即不按照分類Id查
@RequestParam(defaultValue = "1", required = false) int page,
@RequestParam(defaultValue = "6", required = false) int pageSize,
Model model) {
keywords = keywords.trim();
PageInfo<BookInfo> bookPageInfo = bookInfoService.findBookListByCondition(keywords, cateId, page, pageSize,0);//storeId為0,不按照商店Id查詢
model.addAttribute("bookPageInfo", bookPageInfo);
model.addAttribute("keywords", keywords);
model.addAttribute("cateId", cateId);
return "book_list";
}
}
shiro安全框架配置代碼:
@Configuration
/**
* shiro安全框架
*/
public class ShiroConfig {
@Bean(name = "lifecycleBeanPostProcessor")
public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
return new LifecycleBeanPostProcessor();
}
@Bean
public SecurityManager securityManager(EhCacheManager ehCacheManager) {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
securityManager.setRealm(myShiroRealm());
//securityManager.setRememberMeManager(rememberMeManager());
securityManager.setCacheManager(ehCacheManager);
return securityManager;
}
@Bean
public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(securityManager);
Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
//攔截器
filterChainDefinitionMap.put("/img/**", "anon");
filterChainDefinitionMap.put("/fonts/**", "anon");
filterChainDefinitionMap.put("/static/**", "anon");
filterChainDefinitionMap.put("/css/**", "anon");
filterChainDefinitionMap.put("/js/**", "anon");
filterChainDefinitionMap.put("/book/**", "anon");
filterChainDefinitionMap.put("/upload/**", "anon");
filterChainDefinitionMap.put("/page/**", "anon");
filterChainDefinitionMap.put("/user/info", "user");
filterChainDefinitionMap.put("/user/**", "anon");//用戶登錄注冊不需要權限
filterChainDefinitionMap.put("/index/**", "anon");//首頁放行
filterChainDefinitionMap.put("/", "anon");
//配置退出 過濾器,其中的具體的退出代碼Shiro已經替我們實作了
filterChainDefinitionMap.put("/user/logout", "logout");
//<!-- 過濾鏈定義,從上向下順序執行,一般將/**放在最為下邊 -->:這是一個坑呢,一不小心代碼就不好使了;
//<!-- authc:所有url都必須認證通過才可以訪問; anon:所有url都都可以匿名訪問-->
//filterChainDefinitionMap.put("/admin/**", "roles[admin]");//perms[system]
filterChainDefinitionMap.put("/**", "authc");
// 如果不設定默認會自動尋找Web工程根目錄下的"/login.jsp"頁面
shiroFilterFactoryBean.setLoginUrl("/page/login");
// 登錄成功后要跳轉的鏈接
shiroFilterFactoryBean.setSuccessUrl("/index");
//未授權界面;
shiroFilterFactoryBean.setUnauthorizedUrl("/403");
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
return shiroFilterFactoryBean;
}
@Bean
@DependsOn("lifecycleBeanPostProcessor")
public MyShiroRealm myShiroRealm() {
MyShiroRealm myShiroRealm = new MyShiroRealm();
myShiroRealm.setCredentialsMatcher(hashedCredentialsMatcher());
myShiroRealm.setCachingEnabled(true);
//啟用身份驗證快取,即快取AuthenticationInfo資訊,默認false
myShiroRealm.setAuthenticationCachingEnabled(true);
//快取AuthenticationInfo資訊的快取名稱 在ehcache.xml中有對應快取的配置
myShiroRealm.setAuthenticationCacheName("authenticationCache");
//啟用授權快取,即快取AuthorizationInfo資訊,默認false
myShiroRealm.setAuthorizationCachingEnabled(true);
//快取AuthorizationInfo資訊的快取名稱 在ehcache.xml中有對應快取的配置
myShiroRealm.setAuthorizationCacheName("authorizationCache");
return myShiroRealm;
}
/**
* 開啟shiro aop注解支持.
* 使用代理方式;所以需要開啟代碼支持;
* @param securityManager
* @return
*/
@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){
AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
return authorizationAttributeSourceAdvisor;
}
@Bean
public DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() {
DefaultAdvisorAutoProxyCreator daap = new DefaultAdvisorAutoProxyCreator();
daap.setProxyTargetClass(true);
return daap;
}
/**
* 憑證匹配器
* (由于我們的密碼校驗交給Shiro的SimpleAuthenticationInfo進行處理了
* )
*
* @return
*/
@Bean
public HashedCredentialsMatcher hashedCredentialsMatcher() {
HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
hashedCredentialsMatcher.setHashAlgorithmName("md5");//散列演算法:這里使用MD5演算法;
hashedCredentialsMatcher.setHashIterations(1);//散列的次數,比如散列兩次,相當于 md5(md5(""));
return hashedCredentialsMatcher;
}
/**
* cookie物件;
* rememberMeCookie()方法是設定Cookie的生成模版,比如cookie的name,cookie的有效時間等等,
* @return
*/
/*@Bean
public SimpleCookie rememberMeCookie(){
//這個引數是cookie的名稱,對應前端的checkbox的name = rememberMe
SimpleCookie simpleCookie = new SimpleCookie("rememberMe");
//如果httyOnly設定為true,則客戶端不會暴露給客戶端腳本代碼,使用HttpOnly cookie有助于減少某些型別的跨站點腳本攻擊;
simpleCookie.setHttpOnly(true);
//記住我cookie生效時間,默認30天 ,單位秒:60 * 60 * 24 * 30
//<!-- 記住我cookie生效時間30天 ,單位秒;-->
simpleCookie.setMaxAge(1800);
return simpleCookie;
}*/
/**
* cookie管理物件;
* rememberMeManager()方法是生成rememberMe管理器,而且要將這個rememberMe管理器設定到securityManager中
* @return
*/
/*@Bean
public CookieRememberMeManager rememberMeManager(){
//System.out.println("ShiroConfiguration.rememberMeManager()");
CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager();
cookieRememberMeManager.setCookie(rememberMeCookie());
//rememberMe cookie加密的密鑰 建議每個專案都不一樣 默認AES演算法 密鑰長度(128 256 512 位)
cookieRememberMeManager.setCipherKey(Base64.decode("2AvVhdsgUs0FSA3SDFAdag=="));
return cookieRememberMeManager;
}*/
/**
* shiro session的管理
*/
/*@Bean
public DefaultWebSessionManager sessionManager() {
DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
sessionManager.setGlobalSessionTimeout(tomcatTimeout*1000);
//設定sessionDao對session查詢,在查詢在線用戶service中用到了
sessionManager.setSessionDAO(sessionDAO());
//配置session的監聽
Collection<SessionListener> listeners = new ArrayList<SessionListener>();
listeners.add(new BDSessionListener());
sessionManager.setSessionListeners(listeners);
//設定在cookie中的sessionId名稱
sessionManager.setSessionIdCookie(simpleCookie());
return sessionManager;
}*/
@Bean
@DependsOn("lifecycleBeanPostProcessor")
public EhCacheManager ehCacheManager(CacheManager cacheManager) {
EhCacheManager em = new EhCacheManager();
//將ehcacheManager轉換成shiro包裝后的ehcacheManager物件
em.setCacheManager(cacheManager);
em.setCacheManagerConfigFile("classpath:ehcache.xml");
return em;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/354666.html
標籤:其他
上一篇:Java專案:IT設備固定資產管理系統(java+SSM+jsp+mysql+maven)
下一篇:JS案例之放大鏡
