Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
寻找一个简单的Spring安全示例_Spring_Spring Mvc_Spring Security - Fatal编程技术网

寻找一个简单的Spring安全示例

寻找一个简单的Spring安全示例,spring,spring-mvc,spring-security,Spring,Spring Mvc,Spring Security,我是spring security(Java)的新手,我正在寻找一个好的且简单的示例: 如何使用spring安全性进行登录和注销 确保每个页面上都存在会话,如果没有,请重新重定向到登录 如何访问当前用户会话 我的项目目前正在使用spring MVC和hibernate。 我已经构建了loginAPI+loginDAO,现在我需要将安全性结合起来,并对一些页面进行安全保护 我搜索了教程,但其中很多都非常复杂。如果你还没有看过的话。它实际上在Spring安全站点上被引用,但很容易被忽略。虽然我同意,

我是spring security(Java)的新手,我正在寻找一个好的且简单的示例:

  • 如何使用spring安全性进行登录和注销

  • 确保每个页面上都存在会话,如果没有,请重新重定向到登录

  • 如何访问当前用户会话

  • 我的项目目前正在使用spring MVC和hibernate。
    我已经构建了loginAPI+loginDAO,现在我需要将安全性结合起来,并对一些页面进行安全保护


    我搜索了教程,但其中很多都非常复杂。

    如果你还没有看过的话。它实际上在Spring安全站点上被引用,但很容易被忽略。虽然我同意,但很难找到好的Spring安全示例

    嗯。 这是我认为到目前为止我见过的最好的

    您可以在Spring Security中寻找单一登录(例如CAS)实现。这完全符合你的目的

    退房:-


    这也是一个很好的例子:

    它们都有很好的文档记录,并且很容易根据您的提议进行修改。Krams谈到LDAP使用Spring安全性。

    使用的技术:

    弹簧3.2.8.释放
    弹簧安全3.2.3.释放
    Spring JDBC 3.2.3.版本
    Eclipse 4.2
    JDK 1.6
    Maven 3
    Tomcat 6或7(Servlet 3.x)
    MySQL服务器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();
        }
    }
    
    Spring-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
    • 注销成功url=“/login?logout”
      –如果注销成功,请转发至查看
      /logout
    • username parameter=“username”
      –包含“用户名”的请求的名称。在HTML中,这是输入文本的名称
    • –启用跨站点请求伪造(CSRF)保护

    谢谢,听起来不错,但是质量太差了,我在后台看不到代码。请,如果有人能发布一个示例,那就太好了。我正在我的博客上维护代码示例: