登录功能实现
1、修改pom文件,在pom文件中导入spring-security-config、spring-security-web依赖
2、修改web.xml文件,加载spring配置文件,通过contextConfigLocation加载,加载过滤器springSecurityFilterChain==>org.springframework.web.filter.DelegatingFilterProxy
3、添加spring-security.xml,导入dtd,写入不要拦截的页面、拦截规则、认证管理。
配置说明:always-use-default-target:指定了在身份验证后总是跳转到default-target-url,属性指定的url。如果你在系统中使用了框架页,需要设置框架页的策略为SAMEORIGIN
spring-security.xml配置
<http pattern="/login.html" security="none"></http>
<http pattern="/css/**" security="none"></http>
<http pattern="/img/**" security="none"></http>
<http pattern="/js/**" security="none" ></http>
<http pattern="/plugins/**" security="none"></http>
<!-- 页面拦截规则 -->
<http use-expressions="false">
<intercept-url pattern="/*" access="ROLE_ADMIN" />
<form-login login-page="/login.html" default-target-url="/admin/index.html" login-processing-url="/login"
authentication-failure-url="/login.html" always-use-default-target="true" />
<csrf disabled="true" />
<headers>
<frame-options policy="SAMEORIGIN" />
</headers>
</http>
<!-- 认证管理器 -->
<authentication-manager>
<authentication-provider>
<user-service>
<user name="admin" password="123456" authorities="ROLE_ADMIN" />
<user name="sunwukong" password="dasheng" authorities="ROLE_ADMIN" />
</user-service>
</authentication-provider>
</authentication-manager>
其中:login-processing-url="/login",要与<form class="sui-form" action="/login" method="post">相同
4、html页面:id="loginform" action="/login" method="post",在登录名和密码中写“name”属性。提交:onclick="document:loginform.submit()"
获取登录名并在页面上显示
1、在index.html中引入AngularJS
2、在<body>中引入ng-app、controller、init
3、编写loginController.js和loginService.js
app.controller("loginContorller", function($scope, loginService) {
// 获取用户登录名
$scope.loadLoginName = function() {
// 内置服务发送请求
loginService.loadLoginName().success(function(data) {
$scope.loginName = data.loginName;
})
};
})
4、loginController.java中
String name = SecurityContextHolder.getContext().getAuthentication().getName();
将name封装到map集合中,传map
注册商家
1、编写sellerController.js和sellerService.js
2、在html中引入AngularJS,及相关js,在body中引入ng,填写各项ng-model
3、提交将href无效化,添加ng-click调用add方法
4、后台封装seller并返回
注:在新的web中,要注意springMVC配置文件是否与包名对应
商家注册密码加密
1、pom文件中导入security-config、web
2、在sellerController.java add方法中创建BcryptPasswordEncoder对象,获取seller中的password用encode方法创建新的字符串,用set方法重新写入seller并返回
商家审核
一、展示商品列表
1、在manager-web将seller的js、html复制来
2、在html中编写ng-app、controller、init
注意:ng-init中因为要区分审核与未审核的状态,需要展示的是未审核的商家,所以要在页面中过滤出状态为0的商家,所以代码为:ng-init="searchEntity={status:'0'}"
二、商家详情
添加ng-click
三、修改状态
1、在商品审核页面中,修改详情框的三个选项:
<button class="btn btn-success" data-dismiss="modal"
aria-hidden="true" ng-click="updateStatus(entity.sellerId,'1')">审核通过</button>
<button class="btn btn-danger" data-dismiss="modal"
aria-hidden="true" ng-click="updateStatus(entity.sellerId,'2')">审核未通过</button>
2、完成controller.js和service.js【传sellerId和status】
3、编写后台代码
四、商家系统登录与安全控制
1、自定义认证类
(1)Pom.xml导入spring-security config、web依赖,编写web.xml加入安全框架过滤器、login.html
(2) 创建com.pinyougou.service包,包下创建类UserDetailsServiceImpl.java实现UserDetailsService接口
List<GrantedAuthority> grantedAuths = new ArrayList<GrantedAuthority>();
grantedAuths.add(new SimpleGrantedAuthority("ROLE_SELLER"));
return new User(username,"123456", grantedAuths);
(3)S pring 目录下创建 spring-security.xml ,在 manager 下 spring-security.xml 基础上添加 /seller/add 网页不被拦截。添加认证管理器<authentication-manager>
<authentication-provider user-service-ref="userDetailService">
</authentication-provider>
</authentication-manager>
<beans:bean id="userDetailService"
class="com.pinyougou.service.UserDetailServiceImpl"></beans:bean>
经过以上配置,用户输入“123456”可通过
2、认证类调用服务方法
修改UserDetailServiceImpl.java,添加属性和setter方法,修改loadUserByUsername方法
//构建角色列表
List<GrantedAuthority> grantAuths=new ArrayList();
grantAuths.add(new SimpleGrantedAuthority("ROLE_SELLER"));
//得到商家对象
TbSeller seller = sellerService.findOne(username);
if(seller!=null){
if(seller.getStatus().equals("1")){
return new User(username,seller.getPassword(),grantAuths);
}else{
return null;
}else{
return null;
}
修改spring-security.xml
<!-- 引用 dubbo 服务 -->
<dubbo:application name="pinyougou-shop-web" />
<dubbo:registry address="zookeeper://192.168.25.129:2181"/>
<dubbo:reference id="sellerService"
interface="com.pinyougou.sellergoods.service.SellerService" >
</dubbo:reference>
<beans:bean id="userDetailService"
class="com.pinyougou.service.UserDetailsServiceImpl">
<beans:property name="sellerService" ref="sellerService"></bean:property>
</beans:bean>
3、密码加密
(1)修改sellerController add方法
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
String password = passwordEncoder.encode(seller.getPassword());
seller.setPassword(password);
(2)加密配置
Spring-security.xml配置如下
<beans:bean id="bcryptEncoder"
class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" />
修改认证管理器的配置
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref='userDetailService'>
<password-encoder ref="bcryptEncoder"></password-encoder>
</authentication-provider>
</authentication-manager>