请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

为什么验证授权码模式却弹出basic登录框

老师好:

    我想验证授权码模式,我的pom.xml为:

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- oauth2 -->
        <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
        </dependency>
	</dependencies>

启动类为:

@SpringBootApplication
@RestController
public class SecurityApplication {
	
	private final Logger logger = LoggerFactory.getLogger(getClass());

	public static void main(String[] args) {
		SpringApplication.run(SecurityApplication.class, args);
	}
	
	@GetMapping("/test")
	public String hello() {
		logger.info("in controller");
		return "hello spring security";
	}
}

密码加密配置类为:

@Configuration
public class SecurityConfig {
    @Bean
    public PasswordEncoder PasswordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

UserdetailService类为

@Component
public class MyUserdetailsService implements UserDetailsService {

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        // 根据username到数据库中查询用户信息
        return new User(username, passwordEncoder.encode("123456"), true, true, true, true,
                AuthorityUtils.commaSeparatedStringToAuthorityList("admin,ROLE_USER"));
    }

}

授权服务器为

@Configuration
@EnableAuthorizationServer
public class ServerConfig {

}

资源服务器为:

@Configuration
@EnableResourceServer
public class ResourceConfig {

}

然后我想验证授权码登录,请求的URL为:

http://127.0.0.1:8080/oauth/authorize?response_type=code&client_id=clientId_abcd&redirect_uri=test.com&scope=all&state=abc 

结果跳出Basic登录的那个弹窗

但是奇怪的是,我可以验证 密码登录模式,并且可以使用它生成的token请求资源服务器的信息,请问老师这个是为什么?

正在回答

2回答

什么为什么?为什么弹出Basic登录框?因为认证服务器没做任何个性化配置,默认就是Basic认证。

还是为什么可以用密码登录模式?因为你配了UserDetailsService。

3 回复 有任何疑惑可以回复我~
豪学者 2018-08-31 10:46:38

你好,你是怎么解决了?

0 回复 有任何疑惑可以回复我~
  • 提问者 TinyLeon #1
    你好,这个不用解决。授权码认证本来就是要先登录的。
    回复 有任何疑惑可以回复我~ 2018-09-01 20:02:34
  • 豪学者 回复 提问者 TinyLeon #2
    我已经登陆了,但刷新token就会出现这个问题
    回复 有任何疑惑可以回复我~ 2018-09-03 14:46:53
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号