我尝试在谷歌搜索,但我找不到任何好的例子,其中用数据库检查用户名和密码以进行身份验证.
换句话说,如何使用Spring和Hibernate创建一个简单的登录表单,其中使用数据库检查凭据.
更新
Cam有没有想出一个简单的例子,我可以看到流程如何以及输入数据如何传递给hibernate?
简单的问题,我只需要一个正确方向的指针:
我有一个简单的Spring MVC/Spring Security webapp.起初,我成立了春季安全,这样的默认登录页显示,妥善认证(我实现了UserDetailsService与DaoAuthenticationProvider要做到这一点).
下一步:使用我的登录页面替换默认的spring登录页面并发布凭据.
但是我如何处理提交的登录凭据?我假设我将表单发布到控制器,验证凭据,但我不清楚在此之后正确的步骤是什么.例如:
我已经完成了3次文档,并没有完全遵循它们.我知道这很简单,所以我只需要听听过程应该如何流动.
我的意思是需要不同:
针对不同的切入点.
这可能吗?
我正在使用Spring 3.1进行身份验证.
我的要求:
我的春季安全配置:
<sec:http pattern="/resources/**" security="none" />
<sec:http auto-config="true">
<sec:intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />
<sec:intercept-url pattern="/customer/**" access="ROLE_CUSTOMER" />
<sec:intercept-url pattern="/employee/**" access="ROLE_EMPLOYEE" />
</sec:http>
<bean id="springSecurityFilterChain" class="org.springframework.security.web.FilterChainProxy">
<sec:filter-chain-map path-type="ant">
<sec:filter-chain pattern="/**"
filters="authenticationProcessingFilterForCustomer,authenticationProcessingFilterForEmployee" />
</sec:filter-chain-map>
</bean>
<bean id="authenticationProcessingFilterForCustomer"
class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
<property name="authenticationManager" ref="authenticationManagerForCustomer" />
<property name="filterProcessesUrl" value="/j_spring_security_check_for_customer" />
<property name="authenticationSuccessHandler" ref="customerSuccessHandler" />
<property name="authenticationFailureHandler" ref="customerFailureHandler" />
</bean>
<bean id="customerSuccessHandler"
class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
<property name="defaultTargetUrl" value="/customer/index.html" />
</bean>
<bean id="customerFailureHandler"
class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
<property name="defaultFailureUrl" value="/customer.html?login_error=1" />
</bean>
<bean id="authenticationManagerForCustomer"
class="org.springframework.security.authentication.ProviderManager">
<property name="providers">
<list>
<ref bean="customCustomerAuthenticationProvider" …Run Code Online (Sandbox Code Playgroud) 我在Spring 4下使用基本身份验证工作REST API.这些REST服务位于/ api/v1/**URL下.但是,我想在不同的url/api/v2/**下添加另一组REST端点,但使用基于令牌的身份验证进行保护.
是否可以使用一个servlet执行此操作?如何配置Spring Security以对不同的URL使用不同形式的身份验证?
谢谢.
在使用 spring security 时,我查看了 stackoverflow 中有趣的线程,需要针对不同的身份验证提供者对两组用户进行身份验证,例如员工反对LDAP和客户反对DATABASE。Thread 提出了一个公认的解决方案,使用一个单选按钮来区分员工和客户,并具有自定义身份验证过滤器,该过滤器根据用户类型区分登录请求并设置不同的身份验证令牌(customerAuthToken / employeeAuthToken),然后请求进行身份验证。会有两个AuthenticationProvider实现和认证是通过支持令牌完成和决定的。通过这种方式,线程能够提供有趣的解决方案来避免 spring security 默认提供的回退身份验证。
查看线程配置 Spring Security 3.x 以具有多个入口点
由于答案完全在 xml 配置中。我只是想让解决方案在 java 配置中可用。我将在回答中发布该内容。
现在我的问题是,随着 spring 版本的发展,除了我的答案之外,是否有可能通过任何新功能/最小配置来具有相同的功能?
我正在使用Spring Security 3.2.1.RELEASE和Spring MVC 4.0.4.RELEASE
我正在尝试为具有两个不同登录条目页面的Web应用程序设置Spring Security.我需要将页面区分开来,因为它们的样式和访问方式不同.
首次登录页面适用于管理员用户并保护管理员页面/ admin/**
第二个登录页面适用于客户用户并保护客户页面/客户/**.
我试图设置两个WebSecurityConfigurerAdapter子类来配置各个HttpSecurity对象.
CustomerFormLoginWebSecurity正在保护客户页面,如果未经授权,则会重定向到客户登录页面.如果未经授权,AdminFormLoginWebSecurity正在保护管理页面重定向到管理员登录页面.
不幸的是,似乎只强制执行第一个配置.我认为我错过了一些额外的东西来使这两者都起作用.
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Autowired
public void registerGlobalAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("customer").password("password").roles("CUSTOMER").and()
.withUser("admin").password("password").roles("ADMIN");
}
@Configuration
@Order(1)
public static class CustomerFormLoginWebSecurity extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/", "/signin/**", "/error/**", "/templates/**", "/resources/**", "/webjars/**");
}
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/customer/**").hasRole("CUSTOMER")
.and()
.formLogin()
.loginPage("/customer_signin")
.failureUrl("/customer_signin?error=1")
.defaultSuccessUrl("/customer/home")
.loginProcessingUrl("/j_spring_security_check")
.usernameParameter("j_username").passwordParameter("j_password")
.and()
.logout()
.permitAll();
http.exceptionHandling().accessDeniedPage("/customer_signin"); …Run Code Online (Sandbox Code Playgroud)