多学习。

SpringSecurity概述及快速入门——基于配置文件的权限操作

SpringSecurity介绍

SpringSecurity是一款权限框架,为项目组提供安全认证服务。安全主要来源于两个操作:

  • “认证”,是为用户建立一个他所声明的主体。主体一般是指用户,设备或可以在你系统中执行动作的其他系统。
  • “授权”指的是一个用户能否在你的应用中执行某个操作,在到达授权判断之前,身份的主体已经由身份验证过程建立了。

快速入门案例——基于配置文件的权限操作

1.Maven依赖

我的版本为:5.6.3 版本过高导致跳转失败而tomcat版本过低(maven只有tomcat7),建议使用:5.1.4.RELEASE

   <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${spring.security.version}</version>
        </dependency>

<!--下面这些用于基于数据库的权限操作,基于配置文件并不需要-->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>${spring.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
            <version>${spring.security.version}</version>
   </dependency>

2.web.xml配置SpringSecurity过滤器

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath: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>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

3.SpringSecurity配置文件

classpath:spring-security.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:security="http://www.springframework.org/schema/security"
	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">        
          
	
	<!-- 配置不过滤的资源(静态资源及登录相关) -->
	<security:http security="none" pattern="/login.html" />
	<security:http security="none" pattern="/failer.html" />
	<security:http auto-config="true" use-expressions="false" >
		<!-- 配置资料连接,表示任意路径都需要ROLE_USER权限 -->
		<security:intercept-url pattern="/**" access="ROLE_USER" />
		<!-- 自定义登陆页面,login-page 自定义登陆页面 authentication-failure-url 用户权限校验失败之后才会跳转到这个页面,如果数据库中没有这个用户则不会跳转到这个页面。 
			default-target-url 登陆成功后跳转的页面。 注:登陆页面用户名固定 username,密码 password,action:login -->
		<security:form-login login-page="/login.html"
			login-processing-url="/login" username-parameter="username"
			password-parameter="password" authentication-failure-url="/failer.html"
			default-target-url="/success.html" 
			/>
		<!-- 登出, invalidate-session 是否删除session logout-url:登出处理链接 logout-success-url:登出成功页面 
			注:登出操作 只需要链接到 logout即可登出当前用户 -->
		<security:logout invalidate-session="true" logout-url="/logout"
			logout-success-url="/login.jsp" />
		<!-- 关闭CSRF,默认是开启的 -->
		<security:csrf disabled="true" />
	</security:http>
	<security:authentication-manager>
		<security:authentication-provider>
			<security:user-service>
				<security:user name="user" password="{noop}user"
					authorities="ROLE_USER" />
				<security:user name="admin" password="{noop}admin"
					authorities="ROLE_ADMIN" />
			</security:user-service>
		</security:authentication-provider>
	</security:authentication-manager>
</beans>

展示

删除配置文件中的自定义登录页面,即可使用SpringSecurity自动生成的登录页面

配置的ROLE_USER权限,使用admin登录,权限不足:403即权限不足

tomcat版本过低,SpringSecurity版本过高,导致跳转失败:maven运用的是tomcat7,SpringSecurity换成5.1即可

跳转成功

posted @   czyaaa  阅读(73)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示