Java之品优购课程讲义_day04(7)

.商家系统登录与安全控制

5.1 需求分析

完成商家系统登陆与安全控制,商家账号来自数据库,并实现密码加密

5.2 自定义认证类

(1)pom.xml、web.xml        、login.html        参照运营商管理后台

(2)在        pinyougou-shop-web        创  建        com.pinyougou.service        包 , 包 下 创 建 类

UserDetailsServiceImpl.java 实现 UserDetailsService 接口

package  com.pinyougou.service; import  java.util.ArrayList; import  java.util.List;
import  org.springframework.security.core.GrantedAuthority;
 
import  org.springframework.security.core.authority.SimpleGrantedAuthority;
 
import  org.springframework.security.core.userdetails.User;
import  org.springframework.security.core.userdetails.UserDetails;
 
import  org.springframework.security.core.userdetails.UserDetailsService;
 
import  org.springframework.security.core.userdetails.UsernameNotFoundException;
 
/**
 
*认证类
 
*@author  Administrator
 
*
 
*/
 
public  class  UserDetailsServiceImpl  implements  UserDetailsService  { @Override
public  UserDetails  loadUserByUsername(String  username)  throws
UsernameNotFoundException  {
 
List<GrantedAuthority>  grantedAuths  =  new  ArrayList<GrantedAuthority>(); grantedAuths.add(new  SimpleGrantedAuthority("ROLE_SELLER"));
return  new  User(username,"123456",  grantedAuths);
 
}
 
}
(3)在 pinyougou-shop-web 的 spring 目录下创建 spring-security.xml

<!-- 以下页面不被拦截 -->
 
<http  pattern="/*.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  pattern="/seller/add.do"  security="none"></http>
 
<!-- 页面拦截规则 -->
 
<http  use-expressions="false">
 
 
<intercept-url  pattern="/**"  access="ROLE_SELLER"  />
 
 
<form-login  login-page="/shoplogin.html"
default-target-url="/admin/index.html"  authentication-failure-url="/shoplogin.html" always-use-default-target="true"/>
 
<csrf  disabled="true"/>
 
 
<headers>
 
 
<frame-options  policy="SAMEORIGIN"/>
 
 
</headers>
 
 
<logout/>
 
 
</http>
 
<!-- 认证管理器 -->
 
<authentication-manager>
 
 
<authentication-provider  user-service-ref="userDetailService">
 
 
</authentication-provider>
 
 
</authentication-manager>
<beans:bean  id="userDetailService" class="com.pinyougou.service.UserDetailServiceImpl"></beans:bean>
经过上述配置,用户在输入密码 123456 时就会通过(用户名随意)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值