简体   繁体   English

使用自定义登录的Spring Security LDAP身份验证

[英]Spring Security LDAP Authentication with custom login

I don't know how to do some stuff with spring security and I hope you guys can you help me. 我不知道如何使用Spring Security做些事情,希望你们能为我提供帮助。 I have two controllers: one for login page and one for home page 我有两个控制器:一个用于登录页面,一个用于主页

 @RestController
 @RequestMapping("/rest/hello")
 public class LoginController {

   @GetMapping(value="/login")
   public String getLoginPage(Model model){
     return "login";
   }
 }

and

@RestController
@RequestMapping("/rest/hello")
public class HomeController {
   @GetMapping("/home")
   public String getHomePage() {
     return "homePage";
   }
}

For this controllers I have html pages, 对于这个控制器,我有html页面,

login.html 的login.html

     <body>
         <div class="limiter">
         <div class="container-login100">
         <div class="wrap-login100">
            <div class="login100-form-title" style="background-image: url(images/bg-01.png);">

            </div>

            <form class="login100-form validate-form">
                <div class="wrap-input100 validate-input m-b-26" data-validate="Username is required">
                    <span class="label-input100">Username</span>
                    <input class="input100" type="text" name="username" placeholder="Enter username">
                    <span class="focus-input100"></span>
                </div>

                <div class="wrap-input100 validate-input m-b-18" data-validate = "Password is required">
                    <span class="label-input100">Password</span>
                    <input class="input100" type="password" name="pass" placeholder="Enter password">
                    <span class="focus-input100"></span>
                </div>

                <div class="flex-sb-m w-full p-b-30">
                    <div class="contact100-form-checkbox">
                        <input class="input-checkbox100" id="ckb1" type="checkbox" name="remember-me">
                        <label class="label-checkbox100" for="ckb1">
                            Remember me
                        </label>
                    </div>

                    <div>

                    </div>
                </div>

                <div class="container-login100-form-btn">
                    <button class="login100-form-btn">
                        Login
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

ldif file and a configuration that looks like that: ldif文件和如下配置:

@EnableGlobalMethodSecurity
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests()
            .anyRequest().fullyAuthenticated()
            .and()
            .formLogin();
}


@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth
            .ldapAuthentication()
            .userDnPatterns("uid={0},ou=people")
            .groupSearchBase("ou=groups")
            .contextSource(contextSource())
            .passwordCompare()
            .passwordEncoder(new LdapShaPasswordEncoder())
            .passwordAttribute("userPassword");
}

@Bean
public DefaultSpringSecurityContextSource contextSource() {
    return new DefaultSpringSecurityContextSource(Arrays.asList("ldap://localhost:8389/"), 
"dc=springframework,dc=org");
}
}

First of all, I want to use my login form not the default form. 首先,我想使用我的登录表单而不是默认表单。 If I try to do this in my configuration 如果我尝试在我的配置中执行此操作

 .formLogin().loginPage("/login");  

I receive this error: This page isn't working localhost redirected you too many times. 我收到此错误:此页面无法正常运行localhost将您重定向了太多次。 Try clearing your cookies 尝试清除您的Cookie

And my second problem is that after authentication I want to redirect me to the home page but instead I receive the a message with the name of the html file: "homePage" 我的第二个问题是,经过身份验证后,我想将我重定向到主页,但是我收到一条消息,其中包含html文件的名称:“ homePage”

You might try the following. 您可以尝试以下方法。

Change the login REST controller @Get Mapping . 更改登录REST控制器@Get Mapping

 @RestController
 @RequestMapping("/rest/hello")
 public class LoginController {

   @GetMapping(value="/dologin") // <---- new
   public String getLoginPage(Model model){
     return "login";
   }
 }

Match the above "/dologin" to the change in the Http configure method. 将上面的"/dologin"匹配到Http configure方法中的更改。

.formLogin().loginPage("/dologin"); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM