Skip to content

Commit feaf501

Browse files
committed
商家系统登录与安全控制
1 parent 40f6d77 commit feaf501

File tree

4 files changed

+54
-7
lines changed

4 files changed

+54
-7
lines changed

pinyougou-parent/pinyougou-shop-web/src/main/java/com/pinyougou/service/UserDetailsServiceImpl.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,59 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6+
import org.omg.CORBA.PRIVATE_MEMBER;
67
import org.springframework.security.core.GrantedAuthority;
78
import org.springframework.security.core.authority.SimpleGrantedAuthority;
89
import org.springframework.security.core.userdetails.User;
910
import org.springframework.security.core.userdetails.UserDetails;
1011
import org.springframework.security.core.userdetails.UserDetailsService;
1112
import org.springframework.security.core.userdetails.UsernameNotFoundException;
1213

14+
import com.pinyougou.pojo.TbSeller;
15+
import com.pinyougou.sellergoods.service.SellerService;
16+
1317
/**
1418
* 认证类
1519
* @author rxs
1620
*
1721
*/
1822
public class UserDetailsServiceImpl implements UserDetailsService {
1923

24+
private SellerService sellerService;
2025

26+
public SellerService getSellerService() {
27+
return sellerService;
28+
}
29+
30+
public void setSellerService(SellerService sellerService) {
31+
this.sellerService = sellerService;
32+
}
33+
2134
@Override
2235
public UserDetails loadUserByUsername(String username)
2336
throws UsernameNotFoundException {
37+
38+
2439
System.out.println("经过了这个类型方法阿斯顿发");
2540
//构建角色列表
26-
List<GrantedAuthority> grantAuths =new ArrayList<GrantedAuthority>();
41+
List<GrantedAuthority> grantAuths =new ArrayList();
2742
grantAuths.add(new SimpleGrantedAuthority("ROLE_SELLER"));
28-
return new User(username,"123456",grantAuths);
43+
44+
TbSeller seller = sellerService.findOne(username);
45+
if(seller !=null){
46+
if(seller.getStatus().equals("1")){
47+
return new User(username,seller.getPassword(),grantAuths);
48+
49+
}else {
50+
return null;
51+
}
52+
53+
}else {
54+
return null;
55+
}
56+
2957
}
3058

59+
60+
3161
}

pinyougou-parent/pinyougou-shop-web/src/main/java/com/pinyougou/shop/controller/SellerController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.pinyougou.shop.controller;
22
import java.util.List;
33

4+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
45
import org.springframework.web.bind.annotation.RequestBody;
56
import org.springframework.web.bind.annotation.RequestMapping;
67
import org.springframework.web.bind.annotation.RestController;
8+
79
import com.alibaba.dubbo.config.annotation.Reference;
810
import com.pinyougou.pojo.TbSeller;
911
import com.pinyougou.sellergoods.service.SellerService;
@@ -48,6 +50,9 @@ public PageResult findPage(int page,int rows){
4850
*/
4951
@RequestMapping("/add")
5052
public Result add(@RequestBody TbSeller seller){
53+
BCryptPasswordEncoder passwordEncoder=new BCryptPasswordEncoder();
54+
String password = passwordEncoder.encode(seller.getPassword());
55+
seller.setPassword(password);
5156
try {
5257
sellerService.add(seller);
5358
return new Result(true, "增加成功");

pinyougou-parent/pinyougou-shop-web/src/main/resources/spring/spring-security.xml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<beans:beans xmlns="http://www.springframework.org/schema/security"
3-
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns:beans="http://www.springframework.org/schema/beans"
4+
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
45
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
6+
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
57
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
68

79

@@ -16,9 +18,9 @@
1618
<!-- 页面的拦截规则 use-expressions:是否启动SPEL表达式 默认是true -->
1719
<http use-expressions="false">
1820
<!-- 当前用户必须拥有ROLE_USER的角色 才可以访问更目录及所属子目录的资源 -->
19-
<intercept-url pattern="/*" access="ROLE_SELLER"/>
21+
<intercept-url pattern="/**" access="ROLE_SELLER"/>
2022
<!-- 开启表单登录功能 -->
21-
<form-login login-page="/shoplogin.html" default-target-url="/admin/index.html" authentication-failure-forward-url="/shoplogin.html" always-use-default-target="true"/>
23+
<form-login login-page="/shoplogin.html" default-target-url="/admin/index.html" authentication-failure-url="/shoplogin.html" always-use-default-target="true"/>
2224
<csrf disabled="true"/>
2325
<headers>
2426
<frame-options policy="SAMEORIGIN"/>
@@ -29,12 +31,20 @@
2931
<authentication-manager>
3032
<!-- 认证的提供者 -->
3133
<authentication-provider user-service-ref="userDetailService">
32-
34+
<password-encoder ref="bcryptEncoder"></password-encoder>
3335
</authentication-provider>
3436
</authentication-manager>
3537

3638
<!-- 认证类 -->
3739
<beans:bean id="userDetailService" class="com.pinyougou.service.UserDetailsServiceImpl">
40+
<beans:property name="sellerService" ref="sellerService" ></beans:property>
3841
</beans:bean>
3942

43+
<!-- 引用dubbo 服务 -->
44+
<dubbo:application name="pinyougou-shop-web" />
45+
<dubbo:registry address="zookeeper://192.168.25.135:2181" timeout="50000"/>
46+
<dubbo:reference id="sellerService" interface="com.pinyougou.sellergoods.service.SellerService"></dubbo:reference>
47+
48+
49+
<beans:bean id="bcryptEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"></beans:bean>
4050
</beans:beans>

pinyougou-parent/pinyougou-shop-web/src/main/resources/spring/springmvc.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
<!-- 引用dubbo 服务 -->
2727
<dubbo:application name="pinyougou-shop-web" />
2828
<dubbo:registry address="zookeeper://192.168.25.135:2181" timeout="50000"/>
29-
<dubbo:annotation package="com.pinyougou.shop.controller" />
29+
<dubbo:annotation package="com.pinyougou.shop.controller" />
30+
31+
3032

3133
</beans>

0 commit comments

Comments
 (0)