Spring Security入门

**

一、固定账号登录

**

  1. 在项目中加入依赖包

         <dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-web</artifactId>		
    <version>4.1.0.RELEASE</version>
    
          <dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-config</artifactId>	
    <version>4.1.0.RELEASE</version>	
     </dependency>
    

2.在web.xml加入拦截器配置

<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:spring/spring-security.xml</param-value>
 </context-param>
 <listener>
	<listener-class>
		org.springframework.web.context.ContextLoaderListener
	</listener-class>
 </listener>	
 <filter>  
	<filter-name>springSecurityFilterChain</filter-name>  		
	<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
 </filter>  
 <filter-mapping>  
	<filter-name>springSecurityFilterChain</filter-name>  
	<url-pattern>/*</url-pattern>  
 </filter-mapping>	

3.在配置文件中加入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" 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-service>		
	</authentication-provider>	
</authentication-manager>

4.登录页的配置

在登录页的from表单中加入action属性,值为/login,并且method设置为post;
*** /login 此路径为spring security框架默认自动生成,不需要配置,也可以修改,需要在配置文件中form-login标签中加入属性login-processing-url中设置
表单中用户名和密码输入框中加入name属性,值分别为username和password;
同样,如果要修改的话需要在配置文件中form-login标签中加入属性username-parameter或者password-parameter;

到此配置完成。
如果登录按钮为超链接,并且不想修改为按钮的话,可以给from表单加入id属性,在超链接标签内直接加入onclick事件。如:οnclick=“document:loginform.submit()”

补充配置文件头信息

<?xml version="1.0" encoding="UTF-8"?>

<beans:beans xmlns=“http://www.springframework.org/schema/security
xmlns:beans=“http://www.springframework.org/schema/beans” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation=“http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd”>

二、查询数据库登录

1.修改认证管理器并编写认证类

a. 修改认证管理器
 <authentication-manager>
	<authentication-provider user-service-ref="userDetailService">			
	</authentication-provider>	
</authentication-manager>
<beans:bean id="userDetailService" class="com.lpk.service.UserDetailServiceImpl"></beans:bean>    
b. 编写认证类

创建类UserDetailsServiceImpl.java并实现UserDetailsService接口

 @Override
 public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
 
 	//构建角色列表
 	List<GrantedAuthority> grantAuths=new ArrayList();
 	grantAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
 	
 	TbSeller seller = sellerService.findOne(username);
 	if(seller!=null){
 			return new User(username,seller.getPassword(),grantAuths);
 	}else{
 		return null;
 	}
 }
}	

三、使用BCrypt加密算法

1. 在添加用户时将密码进行编码。
  BCryptPasswordEncoder passwordEncoder = new   BCryptPasswordEncoder();
	String password = passwordEncoder.encode(user.getPassword());
	user.setPassword(password);
2. 修改配置文件。

<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>  
本项目是一个基于SSM(Spring+SpringMVC+MyBatis)框架和Vue.js前端技术的家教平台系统。该系统旨在为家教和学生提供一个便捷、高效的在线交流和预约平台,涵盖了从用户注册登录、个人信息管理、课程发布与搜索、预约与取消预约、评价反馈等一系列功能。 在后台管理方面,系统提供了管理员对用户信息、课程信息、预约记录等进行管理的功能,确保平台的正常运行和数据的准确性。通过Spring框架的依赖注入和AOP特性,实现了业务逻辑的清晰分离和高效处理;SpringMVC则负责处理前端请求和响应,提供友好的用户界面;MyBatis作为ORM框架,简化了数据库操作,提高了数据访问的效率和安全性。 前端部分采用Vue.js框架,结合Vue Router进行页面路由管理,Axios进行HTTP请求,实现了前后端分离的开发模式。Vue.js的组件化开发和响应式数据绑定特性,使得前端页面更加动态和交互性强,提升了用户体验。 数据库设计采用了MySQL,存储了用户信息、课程信息、预约记录等核心数据。通过合理的数据库表结构和索引设计,保证了系统的高效运行和数据的一致性。 该项目不仅适合计算机相关专业的毕设学生参考和学习,也适合Java学习者进行项目实战练习。通过对该项目的深入理解和二次开发,可以实现更多个性化功能,进一步提升技术水平和实践能力。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值