[DELVE][web]基于angular js的Spring security验证
来源:互联网 发布:广东粤数大数据 编辑:程序博客网 时间:2024/04/26 09:04
REQUIREMENT
现在做的项目,前台是angular js,后台是基于spring的java web。
两者是完全分离开的。身份验证是首次传入帐后密码,之后用token进行验证。
Spring security的默认配置是应用在jsp条件,前后台耦合的情况下。适用于前后分离框架的spring-security配置需要花番功夫。
stackoverflow上的一个提问
github上基于rest的spring-security配置示例工程
STEPS
之前我的项目中已经实现了spring,mybatis和aop,接下来与spring架构本身有关的配置忽略了哈,这里只讨论在原有的spring配置基础上加入spring-security。
0、引入包,通过maven添加dependency,其他引入方式见官方文档
<dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>4.0.4.RELEASE</version></dependency><dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>4.0.4.RELEASE</version></dependency>
1、web.xml 配置—加入filter
<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>
2、web.xml 配置—设置spring-security的配置文件路径
<!--spring-security的配置文件的路径设置必须放在这里,试了下放<servlet>下配置会报错--><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>
3、spring-security.xml文件内容。一些可能会用到的功能我做了注释。
<?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-4.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd"> <http use-expressions="true" auto-config="false" entry-point-ref="CustomAuthenticationEntryPoint"> <!-- 自定义所有返回报文的header <headers > <header name="customerHeaderName" value="customerHeaderValue"/> </headers> 对固定模式的url访问进行权限控制,url命名风格为ant <intercept-url pattern="/user/**" access="hasIpAddress('192.168.1.0/24')" /> --> <custom-filter ref="authenticationTokenProcessingFilter" position="PRE_AUTH_FILTER" /> </http> <beans:bean id="CustomAuthenticationEntryPoint" class="com.wnlo.ams.controller.common.CustomAuthenticationEntryPoint" /> <beans:bean id="authenticationTokenProcessingFilter" class="com.wnlo.ams.controller.common.AuthenticationTokenProcessingFilter" /> <authentication-manager alias="authenticationManager" erase-credentials="true"/> <!-- 允许使用@Secured注解 --> <global-method-security secured-annotations="enabled"> <!-- 对指定范围的包进行权限控制 --> <!-- <protect-pointcut access="" expression="execution(* com.mycompany.*Service.*(..))"/> --> </global-method-security></beans:beans>
4.spring-security.xml的配置中最重要的就是两个bean,代码贴出来太麻烦了,链接如下。
CustomAuthenticationEntryPoint
authenticationTokenProcessingFilter
5.这样就ok了,程序已经可以进行测试。
在方法上加@secured注解控制对方法的访问权限,如:@secured(“ROLE_ADMIN”)表示只有拥有ADMIN权限的用户才能访问。
用spring-security.xml中的intercept-url关键字控制对特定url的访问权限,这个上文有示例。
通过修改authenticationTokenProcessingFilter.java我们可以自定义根据客户端发送的token,赋予访问者相应的权限。
通过修改CustomAuthenticationEntryPoint.java我们可以自定权限验证未通过后的处理办法。
至此大体的用户赋权和对权限的控制框架已经搭好了,更多spring-security配置的内容可以去看官方文档,这里就不再说啦。
- [DELVE][web]基于angular js的Spring security验证
- Spring Security and Angular JS
- Spring Security and Angular JS
- Spring Security 和 Angular JS(1)
- Spring Security 和 Angular JS(2)
- Spring Security + JWT 实现基于Token的安全验证
- The Login Page: Angular JS and Spring Security Part II
- The Resource Server: Angular JS and Spring Security Part III
- SSO with OAuth2: Angular JS and Spring Security Part V
- Modularizing the Client: Angular JS and Spring Security Part VII
- SpringBoot入门系列:Spring Security 和 Angular JS(1)
- SpringBoot入门系列:Spring Security 和 Angular JS(2)
- 简单的基于spring security的基于角色验证的权限案例
- Testing an Angular Application: Angular JS and Spring Security Part VIII
- spirng-boot中,基于既有的token验证方式,利用spring-security实现权限系统
- 基于Spring Security实现手机验证码登录
- 使用spring security 实现权限的验证
- Spring Security的HTTP基本验证示例
- 151. Reverse Words in a String
- Spring读书笔记-----部署我的第一个Spring项目
- spring面试题
- 《了不起的盖茨比》有感
- Bean
- [DELVE][web]基于angular js的Spring security验证
- Udp投票通信例子
- Spring 读书笔记-----使用Spring容器(一)
- Matlab 存储图片
- Windows下Redis的安装使用
- JS return false 与 return true(解决表单重复提交的问题)
- JVM学习之:GC的算法简述
- 入门Android开发--笔记--单例原理
- python爬虫爬取涂鸦吧照片