我是spring-security(Java)的新手,我正在寻找一个好的简单示例:
如何使用spring security登录和注销
确保每个页面上都存在会话,如果没有再次重定向到登录
如何访问当前用户会话
我的项目目前正在使用spring MVC和hibernate 我已经构建了loginAPI + loginDAO,我现在需要结合安全性并使一些页面受到保护。
我搜索了教程,但其中很多都非常复杂。
答案 0 :(得分:16)
好。 这是我认为到目前为止我见过的最好的! http://krams915.blogspot.com/2010/12/spring-security-mvc-integration_18.html
答案 1 :(得分:5)
您可以在Spring Security中查找单点登录(例如CAS)实现。它完全符合你的目的。
结帐: -
http://static.springsource.org/spring-security/site/docs/3.0.x/reference/cas.html
https://wiki.jasig.org/display/CASC/Using+the+CAS+Client+3.1+with+Spring+Security
答案 2 :(得分:3)
这也是一个很好的例子:
http://www.mkyong.com/spring-security/spring-security-form-login-example/ http://krams915.blogspot.pt/2010/12/spring-security-3-mvc-using-simple-user.html
它们都有详细记录,并且很容易根据您的建议进行修改。 Krams使用Spring Security讨论LDAP。
答案 3 :(得分:1)
如果您还没有观看this video by the lead developer of Spring Security。它实际上是在Spring Security网站上引用的,但它很容易被遗漏。虽然我同意,好的 Spring Security示例很难得到。
答案 4 :(得分:1)
Spring Security Tutorial by MKyong
how to perform database authentication (using both XML and Annotations) in Spring Security.
使用的技术:
春季3.2.8.RELEASE
Spring Security 3.2.3.RELEASE
Spring JDBC 3.2.3.RELEASE
Eclipse 4.2
JDK 1.6
Maven 3
Tomcat 6或7(Servlet 3.x)
MySQL Server 5.6
SecurityConfig.java
package com.mkyong.config;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
DataSource dataSource;
@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource)
.usersByUsernameQuery(
"select username,password, enabled from users where username=?")
.authoritiesByUsernameQuery(
"select username, role from user_roles where username=?");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
.and()
.formLogin().loginPage("/login").failureUrl("/login?error")
.usernameParameter("username").passwordParameter("password")
.and()
.logout().logoutSuccessUrl("/login?logout")
.and()
.exceptionHandling().accessDeniedPage("/403")
.and()
.csrf();
}
}
弹簧security.xml文件
<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-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">
<!-- enable use-expressions -->
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/admin**" access="hasRole('ROLE_ADMIN')" />
<!-- access denied page -->
<access-denied-handler error-page="/403" />
<form-login
login-page="/login"
default-target-url="/welcome"
authentication-failure-url="/login?error"
username-parameter="username"
password-parameter="password" />
<logout logout-success-url="/login?logout" />
<!-- enable csrf protection -->
<csrf/>
</http>
<!-- Select users and user_roles from database -->
<authentication-manager>
<authentication-provider>
<jdbc-user-service data-source-ref="dataSource"
users-by-username-query=
"select username,password, enabled from users where username=?"
authorities-by-username-query=
"select username, role from user_roles where username =? " />
</authentication-provider>
</authentication-manager>
</beans:beans>
/admin
及其子文件夹都受密码保护。login-page=”/login”
- 显示自定义登录表单的页面authentication-failure-url=”/login?error”
- 如果身份验证失败,请转到第/login?error
页logout-success-url=”/login?logout”
- 如果注销成功,请转发查看/logout
username-parameter=”username”
- 包含“用户名”的请求的名称。在HTML中,这是输入文本的名称。<csrf/>
- 启用跨站点请求伪造(CSRF)保护