介绍
今天就跟大家聊聊有关在春天引导中使用Spring security如何实现对CAS进行集成,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
<强> 1。创建工程强>
创建Maven工程:springboot-security-cas
<强> 2。加入依赖
强>
创建工程后,打开pom.xml,在砰的一声。xml中加入以下内容:
& lt; parent> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-starter-parent & lt; version> 1.4.3.RELEASE & lt;/parent> & lt; properties> & lt; project.build.sourceEncoding> UTF-8 & lt; java.version> 1.8 & lt;/java.version> & lt;/properties> & lt; dependencies> & lt; dependency> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-starter & lt;/dependency> & lt; dependency> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-starter-web & lt;/dependency> & lt; !——安全起动酸盐比; & lt; dependency> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-starter-security & lt;/dependency> & lt; !——安全对CAS支持——比; & lt; dependency> & lt; groupId> org.springframework.security & lt; artifactId> spring-security-cas & lt;/dependency> & lt; !——安全标记库比; & lt; dependency> & lt; groupId> org.springframework.security & lt; artifactId> spring-security-taglibs & lt;/dependency> & lt; !——热加载——比; & lt; dependency> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-devtools & lt; optional> true & lt;/dependency> & lt; dependency> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-configuration-processor & lt; optional> true & lt;/dependency> & lt;/dependencies> & lt; build> & lt; plugins> & lt; plugin> & lt; groupId> org.springframework.boot & lt; artifactId> spring-boot-maven-plugin & lt;/plugin> & lt;/plugins> & lt;/build>
<强> 3。创建application.properties 强>
创建应用程序。属性文件,加入以下内容:
# CAS服务地址 cas.server.host.url=http://localhost: 8081/ca # CAS服务登录地址 cas.server.host.login_url=$ {cas.server.host.url}/登录 # CAS服务登出地址 cas.server.host.logout_url=$ {cas.server.host.url}/logout& # 63;服务=$ {app.server.host.url} #应用访问地址 app.server.host.url=http://localhost: 8080 #应用登录地址 app.login.url=/登录 #应用登出地址 app.logout.url=/注销
<强> 4。创建入口启动类(MainConfig) 强>
创建入口启动类MainConfig,完整代码如下:
包com.chengli.springboot; 进口org.springframework.boot.SpringApplication; 进口org.springframework.boot.autoconfigure.SpringBootApplication; 进口org.springframework.security.access.prepost.PreAuthorize; 进口org.springframework.web.bind.annotation.RequestMapping; 进口org.springframework.web.bind.annotation.RestController; @RestController @SpringBootApplication 公开课MainConfig { 公共静态void main (String [] args) { SpringApplication.run (MainConfig.class, args); } @RequestMapping (“/? 公共字符串索引(){ 返回“访问了首页哦”; } @RequestMapping (“/hello") 公共字符串hello () { 返回“不验证哦”; } @PreAuthorize (“hasAuthority(& # 39;测试# 39;)“)//有测试权限的才能访问 @RequestMapping (“/security") 公共字符串安全(){ 返回“hello world security"; } @PreAuthorize (“hasAuthority(& # 39;行政与# 39;)“)//必须要有管理员权限的才能访问 @RequestMapping (“/authorize") 公共字符串授权(){ 返回“有权限访问“; }/* *这里注意的是,测试与管理只是权限编码,可以自己定义一套规则,根据实际情况即可*/}
<强> 5。创建安全配置类(SecurityConfig)